about summary refs log tree commit diff
path: root/third_party/git/t/t0202/test.pl
blob: 2cbf7b95907384b4b4b5ac91f801026810b85f0a (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/perl
use 5.008;
use lib (split(/:/, $ENV{GITPERLLIB}));
use strict;
use warnings;
use POSIX qw(:locale_h);
use Test::More tests => 13;
use Git::I18N;

my $has_gettext_library = $Git::I18N::__HAS_LIBRARY;

ok(1, "Testing Git::I18N with " .
	 ($has_gettext_library
	  ? (defined $Locale::Messages::VERSION
		 ? "Locale::Messages version $Locale::Messages::VERSION"
		 # Versions of Locale::Messages before 1.17 didn't have a
		 # $VERSION variable.
		 : "Locale::Messages version <1.17")
	  : "NO Perl gettext library"));
ok(1, "Git::I18N is located at $INC{'Git/I18N.pm'}");

{
	my $exports = @Git::I18N::EXPORT;
	ok($exports, "sanity: Git::I18N has $exports export(s)");
}
is_deeply(\@Git::I18N::EXPORT, \@Git::I18N::EXPORT_OK, "sanity: Git::I18N exports everything by default");

# prototypes
{
	# Add prototypes here when modifying the public interface to add
	# more gettext wrapper functions.
	my %prototypes = (qw(
		__	$
		__n	$$$
		N__	$
	));
	while (my ($sub, $proto) = each %prototypes) {
		is(prototype(\&{"Git::I18N::$sub"}), $proto, "sanity: $sub has a $proto prototype");
	}
}

# Test basic passthrough in the C locale
{
	local $ENV{LANGUAGE} = 'C';
	local $ENV{LC_ALL}   = 'C';
	local $ENV{LANG}     = 'C';

	my ($got, $expect) = (('TEST: A Perl test string') x 2);

	is(__($got), $expect, "Passing a string through __() in the C locale works");

	my ($got_singular, $got_plural, $expect_singular, $expect_plural) =
		(('TEST: 1 file', 'TEST: n files') x 2);

	is(__n($got_singular, $got_plural, 1), $expect_singular,
		"Get singular string through __n() in C locale");
	is(__n($got_singular, $got_plural, 2), $expect_plural,
		"Get plural string through __n() in C locale");

	is(N__($got), $expect, "Passing a string through N__() in the C locale works");
}

# Test a basic message on different locales
SKIP: {
	unless ($ENV{GETTEXT_LOCALE}) {
		# Can't reliably test __() with a non-C locales because the
		# required locales may not be installed on the system.
		#
		# We test for these anyway as part of the shell
		# tests. Skipping these here will eliminate failures on odd
		# platforms with incomplete locale data.

		skip "GETTEXT_LOCALE must be set by lib-gettext.sh for exhaustive Git::I18N tests", 2;
	}

	# The is_IS UTF-8 locale passed from lib-gettext.sh
	my $is_IS_locale = $ENV{is_IS_locale};

	my $test = sub {
		my ($got, $expect, $msg, $locale) = @_;
		# Maybe this system doesn't have the locale we're trying to
		# test.
		my $locale_ok = setlocale(LC_ALL, $locale);
		is(__($got), $expect, "$msg a gettext library + <$locale> locale <$got> turns into <$expect>");
	};

	my $env_C = sub {
		$ENV{LANGUAGE} = 'C';
		$ENV{LC_ALL}   = 'C';
	};

	my $env_is = sub {
		$ENV{LANGUAGE} = 'is';
		$ENV{LC_ALL}   = $is_IS_locale;
	};

	# Translation's the same as the original
	my ($got, $expect) = (('TEST: A Perl test string') x 2);

	if ($has_gettext_library) {
		{
			local %ENV; $env_C->();
			$test->($got, $expect, "With", 'C');
		}

		{
			my ($got, $expect) = ($got, 'TILRAUN: Perl tilraunastrengur');
			local %ENV; $env_is->();
			$test->($got, $expect, "With", $is_IS_locale);
		}
	} else {
		{
			local %ENV; $env_C->();
			$test->($got, $expect, "Without", 'C');
		}

		{
			local %ENV; $env_is->();
			$test->($got, $expect, "Without", 'is');
		}
	}
}