* Finished getting the unit tests caught up with the AN::Tools module methods.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 8 years ago
parent 6b5185fe61
commit e3f782a60c
  1. 85
      AN/Tools.t
  2. 4
      AN/Tools/Get.pm
  3. 4
      AN/an-tools.xml

@ -11,7 +11,7 @@ our $VERSION = "3.0.0";
our $THIS_FILE = "Tools.t"; our $THIS_FILE = "Tools.t";
# Call in the test module, telling it how many tests to expect to run. # Call in the test module, telling it how many tests to expect to run.
use Test::More tests => 121; use Test::More tests => 199;
# Load my module via 'use_ok' test. # Load my module via 'use_ok' test.
BEGIN BEGIN
@ -142,6 +142,8 @@ like($an->Get->date_and_time({use_time => 1234567890, offset => -31536000}), qr/
# host_uuid # host_uuid
like($an->Get->host_uuid, qr/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/, "Verifying ability to read host uuid."); like($an->Get->host_uuid, qr/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/, "Verifying ability to read host uuid.");
### TODO: How to test Get->switches? ### TODO: How to test Get->switches?
# uuid
like($an->Get->uuid, qr/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/, "Verifying ability to generate a random uuid.");
### AN::Tools::Log tests ### AN::Tools::Log tests
# entry is tested at the start of this test suite. # entry is tested at the start of this test suite.
@ -518,38 +520,51 @@ is($an->Validate->is_ipv4({ip => "256.255.255.255"}), "0", "Verifying that 'Vali
is($an->Validate->is_ipv4({ip => "alteeve.com"}), "0", "Verifying that 'Validate->is_ipv4' recognizes 'alteeve.com' as an invalid IP address."); is($an->Validate->is_ipv4({ip => "alteeve.com"}), "0", "Verifying that 'Validate->is_ipv4' recognizes 'alteeve.com' as an invalid IP address.");
is($an->Validate->is_ipv4({ip => "::1"}), "0", "Verifying that 'Validate->is_ipv4' recognizes '::1' as an invalid IP address."); is($an->Validate->is_ipv4({ip => "::1"}), "0", "Verifying that 'Validate->is_ipv4' recognizes '::1' as an invalid IP address.");
my $test_uuid = $an->Get->uuid; my $test_uuid = $an->Get->uuid;
is($an->Validate->is_uuid({uuid => $test_uuid}), "1", "Verifying that 'Validate->is_uuid' recognized: [".$test_uuid."] as a valid UUID.");
die; my $bad_uuid_1 = $test_uuid;
$bad_uuid_1 =~ s/-//g;
is($an->Validate->is_uuid({uuid => $bad_uuid_1}), "0", "Verifying that 'Validate->is_uuid' recognized: [".$bad_uuid_1."] as an invalid UUID.");
my $bad_uuid_2 = uc($test_uuid);
is($an->Validate->is_uuid({uuid => $bad_uuid_2}), "0", "Verifying that 'Validate->is_uuid' recognized: [".$bad_uuid_2."] as an invalid UUID.");
my $bad_uuid_3 = $test_uuid."toolong";
is($an->Validate->is_uuid({uuid => $bad_uuid_3}), "0", "Verifying that 'Validate->is_uuid' recognized: [".$bad_uuid_3."] as an invalid UUID.");
### AN::Tools::Words tests ### AN::Tools::Words tests
# # clean_spaces
my $clean_string1 = " A line with spaces all over ";
die; my $clean_string2 = "A line with spaces at the end only ";
my $clean_string3 = " A line with spaces in the front only";
my $clean_string4 = "A line with spaces in the middle only";
is($an->Words->clean_spaces({string => $clean_string1}), "A line with spaces all over", "Verifying that 'Words->clean_spaces' cleaned up a string with random spaces.");
is($an->Words->clean_spaces({string => $clean_string2}), "A line with spaces at the end only", "Verifying that 'Words->clean_spaces' cleaned up a string spaces at the end of a string.");
### AN::Tools::Words methods is($an->Words->clean_spaces({string => $clean_string3}), "A line with spaces in the front only", "Verifying that 'Words->clean_spaces' cleaned up a string with spaces in the front only.");
# Make sure we can read words files is($an->Words->clean_spaces({string => $clean_string4}), "A line with spaces in the middle only", "Verifying that 'Words->clean_spaces' cleaned up a string with spaces in the middle only.");
# key
is($an->Words->key({key => "t_0001"}), "Test replace: [#!variable!test!#].", "Verifying that 'Words->key' returned the Canadian English 't_0001' string.");
is($an->Words->key({key => "t_0001", language => "jp"}), "テスト いれかえる: [#!variable!test!#]。", "Verifying that 'Words->read' returned the Japanese 't_0001' string.");
is($an->Words->key({key => "bad_key"}), "#!not_found!#", "Verified that 'Words->key' returns '#!not_found!#' for a bad key.");
is($an->Words->key({key => "t_0003", language => "jp"}), "#!not_found!#", "Verifying that 'Words->read' returned '#!not_found!#' for the missing 't_0003' key.");
# language
is($an->Words->language, "en_CA", "Verifying the default words language is 'en_CA'.");
$an->Words->language({set => "jp"});
is($an->Words->language, "jp", "Verifying the words language was changed to 'jp'.");
$an->Words->language({set => "en_CA"});
is($an->Words->language, "en_CA", "Verifying the words language is back to 'en_CA'.");
# read
### NOTE: At this time, we don't test for unreadable files (rc = 3) or general read faults as set by XML::Simple (rc = 4).
is($an->Words->read({file => $an->data->{path}{words}{'an-tools.xml'}}), 0, "Verifying that 'Words->read' properly returned '0' when asked to read the AN::Tools's words file."); is($an->Words->read({file => $an->data->{path}{words}{'an-tools.xml'}}), 0, "Verifying that 'Words->read' properly returned '0' when asked to read the AN::Tools's words file.");
is($an->Words->read({file => ''}), 1, "Verifying that 'Words->read' properly returned '1' when asked to read a works file without a file being passed."); is($an->Words->read({file => ''}), 1, "Verifying that 'Words->read' properly returned '1' when asked to read a works file without a file being passed.");
is($an->Words->read({file => '/tmp/dummy.xml'}), 2, "Verifying that 'Words->read' properly returned '2' when asked to read a non-existent file."); is($an->Words->read({file => '/tmp/dummy.xml'}), 2, "Verifying that 'Words->read' properly returned '2' when asked to read a non-existent file.");
### NOTE: At this time, we don't test for unreadable files (rc = 3) or general read faults as set by XML::Simple (rc = 4). # string
my $test_string1 = $an->Words->string({
# Make sure we can read raw strings. key => "t_0005",
is($an->Words->key({key => 't_0001'}), "Test replace: [#!variable!test!#].", "Verifying that 'Words->key' returned the Canadian English 't_0001' string.");
is($an->Words->key({key => 't_0001', language => 'jp'}), "テスト いれかえる: [#!variable!test!#]。", "Verifying that 'Words->read' returned the Japanese 't_0001' string.");
is($an->Words->key({key => 't_0003', language => 'jp'}), "#!not_found!#", "Verifying that 'Words->read' returned '#!not_found!#' for the missing 't_0003' key.");
# Make sure we can read processed strings.
is($an->Words->string({
key => 't_0005',
variables => { variables => {
test => "result!", test => "result!",
first => "1st", first => "1st",
second => "2nd", second => "2nd",
}, },
}), " });
is($test_string1, "
This is a multi-line test string with various items to insert. This is a multi-line test string with various items to insert.
It also has some #!invalid!# replacement #!keys!# to test the escaping and restoring. It also has some #!invalid!# replacement #!keys!# to test the escaping and restoring.
@ -559,15 +574,16 @@ Here we will inject 't_0000': [Test replace: [result!].]
Here we will inject 't_0002' with its embedded variables: [Test Out of order: [2nd] replace: [1st].] Here we will inject 't_0002' with its embedded variables: [Test Out of order: [2nd] replace: [1st].]
Here we will inject 't_0006', which injects 't_0001' which has a variable: [This string embeds 't_0001': [Test replace: [result!].]]. Here we will inject 't_0006', which injects 't_0001' which has a variable: [This string embeds 't_0001': [Test replace: [result!].]].
", "Verifying string processing in the default (Canadian English) language."); ", "Verifying string processing in the default (Canadian English) language.");
is($an->Words->string({ my $test_string2 = $an->Words->string({
language => 'jp', language => "jp",
key => 't_0005', key => "t_0005",
variables => { variables => {
test => "result!", test => "result!",
first => "1st", first => "1st",
second => "2nd", second => "2nd",
}, },
}), " });
is($test_string2, "
これは挿入するさまざまな項目を含む複数行のテスト文字列です これは挿入するさまざまな項目を含む複数行のテスト文字列です
#!無効#!な置換!#キー!#を使ってエスケープとリストアをテストすることもできます。 #!無効#!な置換!#キー!#を使ってエスケープとリストアをテストすることもできます。
@ -578,19 +594,6 @@ is($an->Words->string({
ここでは変数 この文字列にはt_0001が埋め込まれていますテスト いれかえる: [result!]を持つ t_0001を注入する t_0006を注入します ここでは変数 この文字列にはt_0001が埋め込まれていますテスト いれかえる: [result!]を持つ t_0001を注入する t_0006を注入します
", "Verifying string processing in Japanese."); ", "Verifying string processing in Japanese.");
### Get tests. ### DONE!
### Logging
# TODO: How to check logs in journalctl?
### Template
my $active_skin = $an->Template->skin;
### Validate
is($an->Validate->is_uuid({uuid => $an->Get->host_uuid}), "1", "Verifying that Validate->is_uuid() validates the host UUID.");
is($an->Validate->is_uuid({uuid => uc($an->Get->host_uuid)}), "0", "Verifying that Validate->is_uuid() fails to validates the host UUID in upper case.");
is($an->Validate->is_uuid({uuid => "cfdab37c-5b1a-433d-9285-978e7caa134"}), "0", "Verifying that Validate->is_uuid() fails to validates a UUID missing a letter.");
is($an->Validate->is_uuid({uuid => "cfdab37c5b1a433d9285-978e7caa1342"}), "0", "Verifying that Validate->is_uuid() fails to validate a UUID without hyphens.");
# Tell the user that we're done making noise in their logs # Tell the user that we're done making noise in their logs
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0049"}); $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0049"});

@ -371,8 +371,8 @@ sub uuid
my $self = shift; my $self = shift;
my $an = $self->parent; my $an = $self->parent;
my $uuid = $an->System->call({shell_call => $an->data->{path}{exe}{uuidgen}}); my $uuid = $an->System->call({shell_call => $an->data->{path}{exe}{uuidgen}." --random"});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { uuid => $uuid }}); $an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { uuid => $uuid }});
return($uuid); return($uuid);
} }

@ -84,7 +84,7 @@ This is a multi-line test string with various items to insert.
It also has some #!invalid!# replacement #!keys!# to test the escaping and restoring. It also has some #!invalid!# replacement #!keys!# to test the escaping and restoring.
Here is the default output language: [#!data!defaults::languages::output!#] Here is the default output language: [#!data!defaults::language::output!#]
Here we will inject 't_0000': [#!string!t_0001!#] Here we will inject 't_0000': [#!string!t_0001!#]
Here we will inject 't_0002' with its embedded variables: [#!string!t_0002!#] Here we will inject 't_0002' with its embedded variables: [#!string!t_0002!#]
Here we will inject 't_0006', which injects 't_0001' which has a variable: [#!string!t_0006!#]. Here we will inject 't_0006', which injects 't_0001' which has a variable: [#!string!t_0006!#].
@ -106,7 +106,7 @@ Here we will inject 't_0006', which injects 't_0001' which has a variable: [#!st
#!無効!#な置換#!キー!#を使ってエスケープとリストアをテストすることもできます。 #!無効!#な置換#!キー!#を使ってエスケープとリストアをテストすることもできます。
デフォルトの出力言語は次のとおりです:「#!data!defaults::languages::output!#」 デフォルトの出力言語は次のとおりです:「#!data!defaults::language::output!#」
ここで、「t_0000」を挿入します:[#!string!t_0001!#] ここで、「t_0000」を挿入します:[#!string!t_0001!#]
ここでは、 「t_0002」に埋め込み変数を挿入します:「#!string!t_0002!#」 ここでは、 「t_0002」に埋め込み変数を挿入します:「#!string!t_0002!#」
ここでは変数 「#!string!t_0006!#」を持つ 「t_0001」を注入する 「t_0006」を注入します。 ここでは変数 「#!string!t_0006!#」を持つ 「t_0001」を注入する 「t_0006」を注入します。

Loading…
Cancel
Save