summaryrefslogtreecommitdiff
path: root/lib/test/test-scanner.pl
blob: d29f3b40cfe77ec82a025eb5ae8d465d51a0f42b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl -w

# test-scanner.pl - test script for the libsensors config-file scanner
# Copyright (C) 2006 Mark M. Hoffman <mhoffman@lightlink.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; version 2 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

require 5.004;

use strict;
use Test::More;
use Test::Cmd;

my ($scenario, $test);
my @scenarios = (
	{ base => 'empty', status => 0,
		desc => 'empty file' },
	{ base => 'comment', status => 0,
		desc => 'one comment line' },
	{ base => 'comment-without-eol', status => 0,
		desc => 'one comment line, without a trailing newline' },
	{ base => 'keywords', status => 0,
		desc => 'keywords with various whitespace variations' },
	{ base => 'non-keywords', status => 0,
		desc => 'various invalid keyword scenarios' },
	{ base => 'names', status => 0,
		desc => 'normal, unquoted names' },
	{ base => 'names-errors', status => 0,
		desc => 'invalid, unquoted names' },
);

plan tests => ($#scenarios + 1) * 3;

$test = Test::Cmd->new(prog => 'test-scanner', workdir => '');

foreach $scenario (@scenarios) {
	my ($filename, @stdin, @stdout, @expout, @stderr, @experr, @diff);

	$filename = $scenario->{"base"} . ".conf";
	open INPUT, "< $filename" or die "Cannot open $filename: $!";
	@stdin = <INPUT>;
	close INPUT or die "Cannot close $filename: $!";

	$filename = $scenario->{"base"} . ".conf.stdout";
	open OUTPUT, "< $filename" or die "Cannot open $filename: $!";
	@expout = <OUTPUT>;
	close OUTPUT or die "Cannot close $filename: $!";

	# if stderr file is not present, assume none is expected
	$filename = $scenario->{"base"} . ".conf.stderr";
	if (open ERROR, "< $filename") {
		@experr = <ERROR>;
		close ERROR or die "Cannot close $filename: $!";
	} else {
		@experr = ();
	}

	$test->string($scenario->{"desc"});
	$test->run(stdin => \@stdin);

	# test return status
	ok($scenario->{"status"} == $?, "status: " . $scenario->{"desc"});

	# force the captured outputs into an array - for some reason, the
	# 'standard invocation' of diff_exact() chokes without this
	@stdout = $test->stdout;
	@stderr = $test->stderr;

	# test stdout
	ok($test->diff_exact(\@stdout, \@expout, \@diff),
		"stdout: " . $scenario->{"desc"}) or print @diff;

	# test stderr
	ok($test->diff_exact(\@stderr, \@experr, \@diff),
		"stderr: " . $scenario->{"desc"}) or print @diff;
}