From 140819dbb56d4965ef06f00b0229733b56765a39 Mon Sep 17 00:00:00 2001
From: nishiki <nishiki@yaegashi.fr>
Date: Sun, 7 Dec 2014 16:24:31 +0100
Subject: [PATCH] add new unit test

---
 lib/MPW.rb       |   4 +-
 test/test.rb     |  45 --------------------
 test/test_mpw.rb | 105 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+), 47 deletions(-)
 delete mode 100644 test/test.rb
 create mode 100644 test/test_mpw.rb

diff --git a/lib/MPW.rb b/lib/MPW.rb
index 812a24a..a4c5a1d 100644
--- a/lib/MPW.rb
+++ b/lib/MPW.rb
@@ -8,6 +8,7 @@ require 'gpgme'
 require 'csv'
 require 'i18n'
 require 'fileutils'
+require 'yaml'
 	
 module MPW
 	class MPW
@@ -15,7 +16,7 @@ module MPW
 		attr_accessor :error_msg
 		
 		# Constructor
-		def initialize(file_gpg, key=nil, share_keys='')
+		def initialize(file_gpg, key, share_keys='')
 			@error_msg  = nil
 			@file_gpg   = file_gpg
 			@key        = key
@@ -33,7 +34,6 @@ module MPW
 				@data = YAML.load(data_decrypt) if not data_decrypt.to_s.empty?
 			end
 	
-			puts @data.class
 			return true
 		rescue Exception => e 
 			@error_msg = "#{I18n.t('error.gpg_file.decrypt')}\n#{e}"
diff --git a/test/test.rb b/test/test.rb
deleted file mode 100644
index 0f03d1b..0000000
--- a/test/test.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-# File:  tc_simple_number.rb
- 
-require_relative "../lib/MPW"
-require "test/unit"
- 
-class TestMPW < Test::Unit::TestCase
-
-
-	def test_initialize
-		File.delete('test.gpg') if File.exist?('test.gpg')
-	end
-	
-	def test_load_empty_file
-		mpw = MPW::MPW.new('test.cfg')
-		mpw.decrypt
-
-		assert_equal(0, mpw.search.length)
-	end
-
-	def test_add
-		mpw = MPW::MPW.new('test.cfg')
-
-		name     = 'test_name'
-		group    = 'test_group'
-		host     = 'test_host'
-		protocol = 'test_protocol'
-		login    = 'test_login'
-		password = 'test_password'
-		port     = '42'
-		comment  = 'test_comment'
-
-		mpw.update(name, group, host, protocol, login, password, port, comment)
-
-		assert_equal(1, mpw.search.length)
-		assert_equal('test_name', mpw.search[0]['name'])
-		assert_equal('test_group', mpw.search[0]['group'])
-		assert_equal('test_host', mpw.search[0]['host'])
-		assert_equal('test_protocol', mpw.search[0]['protocol'])
-		assert_equal('test_login', mpw.search[0]['login'])
-		assert_equal('test_password', mpw.search[0]['password'])
-		assert_equal(42, mpw.search[0]['port'])
-		assert_equal('test_comment', mpw.search[0]['comment'])
-	end
- 
-end
diff --git a/test/test_mpw.rb b/test/test_mpw.rb
new file mode 100644
index 0000000..bfed5d0
--- /dev/null
+++ b/test/test_mpw.rb
@@ -0,0 +1,105 @@
+# File:  tc_simple_number.rb
+ 
+require_relative "../lib/MPW"
+require "test/unit"
+ 
+class TestMPW < Test::Unit::TestCase
+
+	def setup
+		@file_gpg = 'test.gpg'
+		@key      = 'test-mpw@test-mpw.local'
+
+		if defined?(I18n.enforce_available_locales)
+			I18n.enforce_available_locales = false
+		end
+
+		File.delete(@file_gpg) if File.exist?(@file_gpg)
+		@mpw = MPW::MPW.new(@file_gpg, @key)
+	end
+	
+	def test_load_empty_file
+		assert(@mpw.decrypt)
+		assert_equal(0, @mpw.search.length)
+	end
+
+	def test_add
+		name     = 'test_name'
+		group    = 'test_group'
+		host     = 'test_host'
+		protocol = 'test_protocol'
+		login    = 'test_login'
+		password = 'test_password'
+		port     = '42'
+		comment  = 'test_comment'
+
+		assert(@mpw.update(name, group, host, protocol, login, password, port, comment))
+
+		assert_equal(1, @mpw.search.length)
+
+		result = @mpw.search[0]
+		assert_equal('test_name',     result['name'])
+		assert_equal('test_group',    result['group'])
+		assert_equal('test_host',     result['host'])
+		assert_equal('test_protocol', result['protocol'])
+		assert_equal('test_login',    result['login'])
+		assert_equal('test_password', result['password'])
+		assert_equal(42,              result['port'])
+		assert_equal('test_comment',  result['comment'])
+
+		assert(@mpw.update(name, group, host, protocol, login, password, port, comment))
+
+		assert_equal(2, @mpw.search.length)
+	end
+
+	def test_add_empty_name
+		name     = ''
+		group    = 'test_group'
+		host     = 'test_host'
+		protocol = 'test_protocol'
+		login    = 'test_login'
+		password = 'test_password'
+		port     = '42'
+		comment  = 'test_comment'
+
+		assert(!@mpw.update(name, group, host, protocol, login, password, port, comment))
+
+		assert_equal(0, @mpw.search.length)
+	end
+
+	def test_update
+		name     = 'test_name'
+		group    = 'test_group'
+		host     = 'test_host'
+		protocol = 'test_protocol'
+		login    = 'test_login'
+		password = 'test_password'
+		port     = '42'
+		comment  = 'test_comment'
+
+		assert(@mpw.update(name, group, host, protocol, login, password, port, comment))
+
+		id       = @mpw.search[0]['id']
+		name     = 'test_name_update'
+		group    = 'test_group_update'
+		host     = 'test_host_update'
+		protocol = 'test_protocol_update'
+		login    = 'test_login_update'
+		password = 'test_password_update'
+		port     = '43'
+		comment  = 'test_comment_update'
+
+		assert(@mpw.update(name, group, host, protocol, login, password, port, comment, id))
+		assert_equal(1, @mpw.search.length)
+
+		result = @mpw.search_by_id(id)
+		assert_equal('test_name_update',     result['name'])
+		assert_equal('test_group_update',    result['group'])
+		assert_equal('test_host_update',     result['host'])
+		assert_equal('test_protocol_update', result['protocol'])
+		assert_equal('test_login_update',    result['login'])
+		assert_equal('test_password_update', result['password'])
+		assert_equal(43,                     result['port'])
+		assert_equal('test_comment_update',  result['comment'])
+	end
+ 
+end