source: t/variable.t @ 2e9b9ad

release-1.10
Last change on this file since 2e9b9ad was 4953c44, checked in by David Benjamin <davidben@mit.edu>, 12 years ago
Change set_fromstring to die on failure Returning zero/non-zero is awkward. Leave a note for future improvement to pass the error message along, although the default one is pretty reasonable already.
  • Property mode set to 100644
File size: 3.1 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");
19
20BarnOwl::new_variable_bool("boolvar", { default => 1 });
21is(BarnOwl::getvar("boolvar"), "on", "boolvar default");
22BarnOwl::set("-q", "boolvar", "off");
23is(BarnOwl::getvar("boolvar"), "off", "boolvar set");
24BarnOwl::set("-q", "boolvar", "bogus");
25is(BarnOwl::getvar("boolvar"), "off", "boolvar set bogus");
26BarnOwl::set("-q", "boolvar");
27is(BarnOwl::getvar("boolvar"), "on", "boolvar set");
28BarnOwl::unset("-q", "boolvar");
29is(BarnOwl::getvar("boolvar"), "off", "boolvar unset");
30
31BarnOwl::new_variable_string("strvar", { default => "monkey" });
32is(BarnOwl::getvar("strvar"), "monkey", "strvar default");
33BarnOwl::set("-q", "strvar", "cuttlefish");
34is(BarnOwl::getvar("strvar"), "cuttlefish", "strvar set");
35BarnOwl::set("-q", "strvar");
36is(BarnOwl::getvar("strvar"), "cuttlefish", "strvar set bogus");
37BarnOwl::unset("-q", "strvar");
38is(BarnOwl::getvar("strvar"), "cuttlefish", "strvar unset bogus");
39
40BarnOwl::new_variable_enum("enumvar", { validsettings => [qw/foo bar baz/], default => "bar" });
41is(BarnOwl::getvar("enumvar"), "bar", "enumvar default");
42BarnOwl::set("-q", "enumvar", "baz");
43is(BarnOwl::getvar("enumvar"), "baz", "enumvar set");
44BarnOwl::set("-q", "enumvar", "bogus");
45is(BarnOwl::getvar("enumvar"), "baz", "boolvar set bogus");
46BarnOwl::unset("-q", "enumvar");
47is(BarnOwl::getvar("enumvar"), "baz", "enumvar unset bogus");
48
49BarnOwl::new_variable_int("intvar2");
50is(BarnOwl::getvar("intvar2"), "0", "intvar default default");
51BarnOwl::new_variable_bool("boolvar2");
52is(BarnOwl::getvar("boolvar2"), "off", "boolvar default default");
53BarnOwl::new_variable_string("strvar2");
54is(BarnOwl::getvar("strvar2"), "", "strvar default default");
55BarnOwl::new_variable_enum("enumvar2", { validsettings => [qw/foo bar baz/] });
56is(BarnOwl::getvar("enumvar2"), "foo", "enumvar default default");
57
58# And test new_variable_full
59my $value = "foo";
60BarnOwl::new_variable_full("fullvar", {
61    validsettings => '<short-words>',
62    get_tostring => sub { "value is " . $value },
63    set_fromstring => sub {
64        die "Too long" unless $_[0] =~ /^...?$/;
65        $value = $_[0];
66    },
67    takes_on_off => 1
68});
69is(BarnOwl::getvar("fullvar"), "value is foo", "fullvar get");
70BarnOwl::set("-q", "fullvar", "bar");
71is(BarnOwl::getvar("fullvar"), "value is bar", "fullvar set");
72BarnOwl::set("-q", "fullvar");
73is(BarnOwl::getvar("fullvar"), "value is on", "fullvar set2");
74BarnOwl::unset("-q", "fullvar");
75is(BarnOwl::getvar("fullvar"), "value is off", "fullvar unset");
76BarnOwl::set("-q", "fullvar", "bogus");
77is(BarnOwl::getvar("fullvar"), "value is off", "fullvar set bogus");
78$value = "something really long";
79is(BarnOwl::getvar("fullvar"), "value is something really long", "fullvar set out-of-band");
80
811;
82
Note: See TracBrowser for help on using the repository browser.