source: t/variable.t @ 2497f99

Last change on this file since 2497f99 was 2497f99, checked in by David Benjamin <davidben@mit.edu>, 8 years ago
Add tests to detect new_variable_* overriding previously set values [davidben@mit.edu: Fix some nits]
  • Property mode set to 100644
File size: 4.2 KB
Line 
1#!/usr/bin/env perl
2use strict;
3use warnings;
4
5use Test::More qw(no_plan);
6
7use BarnOwl;
8
9# Test the basic variable types.
10
11BarnOwl::new_variable_int("intvar", { default => 7 });
12is(BarnOwl::getvar("intvar"), "7", "intvar default");
13BarnOwl::set("-q", "intvar", "24");
14is(BarnOwl::getvar("intvar"), "24", "intvar set");
15BarnOwl::set("-q", "intvar", "string");
16is(BarnOwl::getvar("intvar"), "24", "intvar set bogus");
17BarnOwl::unset("-q", "intvar");
18is(BarnOwl::getvar("intvar"), "24", "intvar unset bogus");
19BarnOwl::new_variable_int("intvar", { default => 7 });
20isnt(BarnOwl::getvar("intvar"), "7", "intvar reinit shouldn't override preexisting value");
21
22BarnOwl::new_variable_bool("boolvar", { default => 1 });
23is(BarnOwl::getvar("boolvar"), "on", "boolvar default");
24BarnOwl::set("-q", "boolvar", "off");
25is(BarnOwl::getvar("boolvar"), "off", "boolvar set");
26BarnOwl::set("-q", "boolvar", "bogus");
27is(BarnOwl::getvar("boolvar"), "off", "boolvar set bogus");
28BarnOwl::set("-q", "boolvar");
29is(BarnOwl::getvar("boolvar"), "on", "boolvar set");
30BarnOwl::unset("-q", "boolvar");
31is(BarnOwl::getvar("boolvar"), "off", "boolvar unset");
32BarnOwl::new_variable_bool("boolvar", { default => 1 });
33isnt(BarnOwl::getvar("boolvar"), "on", "boolvar reinit shouldn't override preexisting value");
34
35BarnOwl::new_variable_string("strvar", { default => "monkey" });
36is(BarnOwl::getvar("strvar"), "monkey", "strvar default");
37BarnOwl::set("-q", "strvar", "cuttlefish");
38is(BarnOwl::getvar("strvar"), "cuttlefish", "strvar set");
39BarnOwl::set("-q", "strvar");
40is(BarnOwl::getvar("strvar"), "cuttlefish", "strvar set bogus");
41BarnOwl::unset("-q", "strvar");
42is(BarnOwl::getvar("strvar"), "cuttlefish", "strvar unset bogus");
43BarnOwl::new_variable_string("strvar", { default => "monkey" });
44isnt(BarnOwl::getvar("strvar"), "monkey", "strvar reinit shouldn't override value");
45
46BarnOwl::new_variable_enum("enumvar", { validsettings => [qw/foo bar baz/], default => "bar" });
47is(BarnOwl::getvar("enumvar"), "bar", "enumvar default");
48BarnOwl::set("-q", "enumvar", "baz");
49is(BarnOwl::getvar("enumvar"), "baz", "enumvar set");
50BarnOwl::set("-q", "enumvar", "bogus");
51is(BarnOwl::getvar("enumvar"), "baz", "boolvar set bogus");
52BarnOwl::unset("-q", "enumvar");
53is(BarnOwl::getvar("enumvar"), "baz", "enumvar unset bogus");
54BarnOwl::new_variable_enum("enumvar", { validsettings => [qw/foo bar baz/], default => "bar" });
55isnt(BarnOwl::getvar("enumvar"), "bar", "enumvar reinit shouldn't override value");
56
57BarnOwl::new_variable_int("intvar2");
58is(BarnOwl::getvar("intvar2"), "0", "intvar default default");
59BarnOwl::new_variable_bool("boolvar2");
60is(BarnOwl::getvar("boolvar2"), "off", "boolvar default default");
61BarnOwl::new_variable_string("strvar2");
62is(BarnOwl::getvar("strvar2"), "", "strvar default default");
63BarnOwl::new_variable_enum("enumvar2", { validsettings => [qw/foo bar baz/] });
64is(BarnOwl::getvar("enumvar2"), "foo", "enumvar default default");
65
66# And test new_variable_full
67my $value = "foo";
68BarnOwl::new_variable_full("fullvar", {
69    validsettings => '<short-words>',
70    get_tostring => sub { "value is " . $value },
71    set_fromstring => sub {
72        die "Too long" unless $_[0] =~ /^...?$/;
73        $value = $_[0];
74    },
75    takes_on_off => 1
76});
77is(BarnOwl::getvar("fullvar"), "value is foo", "fullvar get");
78BarnOwl::set("-q", "fullvar", "bar");
79is(BarnOwl::getvar("fullvar"), "value is bar", "fullvar set");
80BarnOwl::set("-q", "fullvar");
81is(BarnOwl::getvar("fullvar"), "value is on", "fullvar set2");
82BarnOwl::unset("-q", "fullvar");
83is(BarnOwl::getvar("fullvar"), "value is off", "fullvar unset");
84BarnOwl::set("-q", "fullvar", "bogus");
85is(BarnOwl::getvar("fullvar"), "value is off", "fullvar set bogus");
86$value = "something really long";
87is(BarnOwl::getvar("fullvar"), "value is something really long", "fullvar set out-of-band");
88# Kinda verbose, but better to test all forms
89my $value = "foo";
90BarnOwl::new_variable_full("fullvar", {
91    validsettings => '<short-words>',
92    get_tostring => sub { "value is " . $value },
93    set_fromstring => sub {
94        die "Too long" unless $_[0] =~ /^...?$/;
95        $value = $_[0];
96    },
97    takes_on_off => 1
98});
99isnt(BarnOwl::getvar("fullvar"), "value is foo", "fullvar reinit doesn't override value");
100
1011;
102
Note: See TracBrowser for help on using the repository browser.