Commits

Herbert Breunung committed de222da

fixed command api tests

Comments (0)

Files changed (3)

lib/Kephra/API.pm

 my $VERSION = 0.1;
 
 
+Kephra::API::Command::switch_variables({
+ '$app'    => { switch => 'app',         class => 'Kephra::App' },
+ '$win'    => { switch => 'main_window', class => 'Kephra::App::Window' },
+ '$docbar' => { switch => 'docbar',      class => 'Kephra::App::Bar::Document' },
+ '$doc'    => { switch => 'document',    class => 'Kephra::Document' },
+ '$ed'     => { switch => 'editor',      class => 'Kephra::App::Editor' },
+});
+Kephra::API::Command::init();
+Kephra::Config::init();
+
+
 sub app             { $Kephra::App::_ref }
 sub main_window     { $Kephra::App::Window::_ref }
 sub editor          { Kephra::API::Docs::active_editor() }
 sub unregister_plugin {}
 
 
-
-Kephra::API::Command::switch_variables({
- '$app'    => { switch => 'app',         class => 'Kephra::App' },
- '$win'    => { switch => 'main_window', class => 'Kephra::App::Window' },
- '$docbar' => { switch => 'docbar',      class => 'Kephra::App::Bar::Document' },
- '$doc'    => { switch => 'document',    class => 'Kephra::Document' },
- '$ed'     => { switch => 'editor',      class => 'Kephra::App::Editor' },
-});
-Kephra::API::Command::init();
-Kephra::Config::init();
-
-
-
 1; # if you want to know more, please read Kephra::Internals.pod
 

lib/Kephra/API/Command.pm

 	my $api = 'Kephra::API';
 	my $caller = calling_module();
 	return Kephra::Log::error("only callable by $api", 1) if $caller ne $api;
-	return Kephra::Log::error("has to be called befor init", 1) if $done_init;
-	return Kephra::Log::error("call me just once", 1) if keys %switchvar;
+	return Kephra::Log::error("has to be called before init", 1)
+		if $done_init and not keys %switchvar;
+	#return Kephra::Log::error("call me just once", 1) if keys %switchvar;
 	return Kephra::Log::error("need a hashref", 1) unless ref $var_def eq 'HASH';
 	for my $var (keys %$var_def){
 		delete $var_def->{$var} unless substr($var, 0, 1) eq '$';

t/610-api-command.t

 BEGIN { unshift @INC, -d 't' ? 'lib' : '../lib' }
 
 package probe;
-use Test::More tests => 14;
+use Test::More tests => 9;
 use Test::NoWarnings;
 
 use Wx;
 
 sub one     { 'one' }
 sub true    { 'true' }
-sub color   { Kephra::App::Util::create_color("blue") }
 sub channel { $_[0] }
 
+package C;
+sub color   { Kephra::App::Util::create_color("blue") }
 
-#package Kephra::API;
+package Kephra::API;
+Kephra::API::Command::switch_variables({
+ '$color'    => { switch => 'colour',   class => 'C' },
+});
+sub colour{ C::color() }
 
-eval { Kephra::API::Command::register_namespace('test') };
-is($@, '', 'registered cmd namespace');
+package probe;
 
-eval { Kephra::API::Command::register_context_var('color') };
-is($@, '', 'registered package switch var');
 
-eval { Kephra::API::Command::register_switch_var({ '$color' => 'color'}) };
-is($@, '', 'registered named switch var');
 
 eval { Kephra::API::Command::register({
-		'test-one' => {sub => 'probe::one', label => 'one'},
-		   '+-true'=> {sub => 'true', label => 'true'},
-		   '+-blue'=> {sub => '$_->IsOk', label => 'blue', help => 'help'},
-		   '+-gc'=>   {sub => '$color->IsOk', label => 'gc'},
-		   '+-chan'=> {sub => 'channel', label => 'channel', parameter => 4},
+	'test-one' => {sub => 'probe::one',   label => 'one'},
+	'test-true'=> {sub => 'true',         label => 'true'},
+	'test-color'=>{sub => 'C::color',     label => 'color'},
+	'test-gc'=>   {sub => '$color->IsOk', label => 'gc'},
+	'test-chan'=> {sub => 'channel',      label => 'channel', parameter => 4},
 })};
-is($@, '', 'registered command');
-
-eval { Kephra::API::Command::init() };
-is($@, '', 'initialized command list');
+is($@, '', 'registered commands');
 
 
 my $data = Kephra::API::Command::all_properties('test-one');
-is(ref $data, 'HASH', 'command entry created');
-is($data->{'label'}, 'one', 'command data stored');
+is(ref $data,                             'HASH', 'command entry created in list');
+is($data->{'label'},                       'one', 'command data retrieved');
 is( Kephra::API::Command::run('test-one'), 'one', 'run command');
 
+
 my $data = Kephra::API::Command::all_properties('test-true');
-is( ref $data,                              'HASH', 'command ID prefix completion');
-is( Kephra::API::Command::run('test-true'), 'true', 'sub prefix completion (module name)');
-is( Kephra::API::Command::run('test-blue'),      1, 'package switch var substitution');
+my $blue = Kephra::App::Util::create_color("blue");
+is( ref $data,                              'HASH', 'package completion when omiting current package name');
+is( Kephra::API::Command::run('test-color')->Blue, $blue->Blue, 'calling different package');
 is( Kephra::API::Command::run('test-gc'),        1, 'named switch var substitution');
 is( Kephra::API::Command::run('test-chan'),      4, 'parameter insertion');
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.