source: t/variable.t @ b9766ea

Last change on this file since b9766ea was 5d8c9a7, checked in by David Benjamin <davidben@mit.edu>, 8 years ago
Make the fullvar variable test more reasonable Calling set on the output of get really should a no-op. Also test that the new version of the setters were installed.
  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[4419d3d]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");
[2497f99]19BarnOwl::new_variable_int("intvar", { default => 7 });
20isnt(BarnOwl::getvar("intvar"), "7", "intvar reinit shouldn't override preexisting value");
[4419d3d]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");
[2497f99]32BarnOwl::new_variable_bool("boolvar", { default => 1 });
33isnt(BarnOwl::getvar("boolvar"), "on", "boolvar reinit shouldn't override preexisting value");
[4419d3d]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");
[2497f99]43BarnOwl::new_variable_string("strvar", { default => "monkey" });
44isnt(BarnOwl::getvar("strvar"), "monkey", "strvar reinit shouldn't override value");
[4419d3d]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");
[2497f99]54BarnOwl::new_variable_enum("enumvar", { validsettings => [qw/foo bar baz/], default => "bar" });
55isnt(BarnOwl::getvar("enumvar"), "bar", "enumvar reinit shouldn't override value");
[4419d3d]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>',
[5d8c9a7]70    get_tostring => sub { $value },
[4419d3d]71    set_fromstring => sub {
[2497f99]72        die "Too long" unless $_[0] =~ /^...?$/;
[5d8c9a7]73        $value = lc($_[0]);
[4419d3d]74    },
75    takes_on_off => 1
76});
[5d8c9a7]77is(BarnOwl::getvar("fullvar"), "foo", "fullvar get");
78BarnOwl::set("-q", "fullvar", "Bar");
79is(BarnOwl::getvar("fullvar"), "bar", "fullvar set");
[4419d3d]80BarnOwl::set("-q", "fullvar");
[5d8c9a7]81is(BarnOwl::getvar("fullvar"), "on", "fullvar set2");
[4419d3d]82BarnOwl::unset("-q", "fullvar");
[5d8c9a7]83is(BarnOwl::getvar("fullvar"), "off", "fullvar unset");
[4419d3d]84BarnOwl::set("-q", "fullvar", "bogus");
[5d8c9a7]85is(BarnOwl::getvar("fullvar"), "off", "fullvar set bogus");
86$value = "xyz";
87is(BarnOwl::getvar("fullvar"), "xyz", "fullvar set out-of-band");
[2497f99]88# Kinda verbose, but better to test all forms
[5d8c9a7]89my $newvalue = "foo";
[2497f99]90BarnOwl::new_variable_full("fullvar", {
91    validsettings => '<short-words>',
[5d8c9a7]92    get_tostring => sub { $newvalue },
[2497f99]93    set_fromstring => sub {
94        die "Too long" unless $_[0] =~ /^...?$/;
[5d8c9a7]95        $newvalue = lc($_[0]);
[2497f99]96    },
97    takes_on_off => 1
98});
[5d8c9a7]99is(BarnOwl::getvar("fullvar"), "xyz", "fullvar reinit doesn't override value");
100$newvalue = "abc";
101is(BarnOwl::getvar("fullvar"), "abc", "fullvar reinit changed setters");
[4419d3d]102
1031;
104
Note: See TracBrowser for help on using the repository browser.