aboutsummaryrefslogtreecommitdiff
path: root/internal/render/ascii_test.go
blob: 7e6a805244cda52557f47751cc6fcf3b31c5c6b0 (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
package render

import (
	"strings"
	"testing"
)

func TestASCIIIsPureASCII(t *testing.T) {
	in := " ! Upał  stopień 1\n 14  29°   (28)   zachmurzenie  ↗\n 30°│███▄▄▁"
	got := ASCII(in, "metric")
	for _, r := range got {
		if r > 127 {
			t.Fatalf("non-ASCII %q survived in %q", r, got)
		}
	}
}

// Substitution runs after layout, so it must not change the character count --
// otherwise every column to the right of a degree sign shears.
func TestASCIIPreservesLength(t *testing.T) {
	for _, in := range []string{
		" 14  29°   (28)   zachmurzenie",
		" 30°│███▄▄▁▂",
		" godz  temp  odczuw  warunki",
		" słońce 12h03m z 14h45m dnia",
	} {
		if got := ASCII(in, "metric"); len([]rune(got)) != len([]rune(in)) {
			t.Errorf("length changed: %q (%d) -> %q (%d)",
				in, len([]rune(in)), got, len([]rune(got)))
		}
	}
}

// IMGW prose already writes "30°C"; the unit letter must not be doubled.
func TestASCIIDoesNotDoubleTheUnitInProse(t *testing.T) {
	got := ASCII("temperatura od 30°C do 33°C", "metric")
	if strings.Contains(got, "CC") {
		t.Fatalf("doubled unit: %q", got)
	}
	if got != "temperatura od 30C do 33C" {
		t.Fatalf("got %q", got)
	}
}

func TestASCIIUsesFahrenheitLetterInImperial(t *testing.T) {
	if got := ASCII("85°", "imperial"); got != "85F" {
		t.Fatalf("got %q, want 85F", got)
	}
}

func TestASCIIMarksUnmappedRunesRatherThanDroppingThem(t *testing.T) {
	if got := ASCII("東京", "metric"); got != "??" {
		t.Fatalf("got %q, want ??", got)
	}
}