Example #1
0
func (test translateTest) run(t *testing.T) {
	cs := charset.Info(test.charset)
	if cs == nil {
		t.Fatalf("no info found for %q", test.charset)
	}
	fromtr, err := charset.TranslatorFrom(test.charset)
	if err != nil {
		t.Fatalf("error making translator from %q: %v", test.charset, err)
	}
	out, err := translate(fromtr, test.in)
	if err != nil {
		t.Fatalf("error translating from %q: %v", test.charset, err)
	}
	if out != test.out {
		t.Fatalf("error translating from %q: expected %x got %x", test.charset, test.out, out)
	}

	if cs.NoTo || !test.canRoundTrip {
		return
	}

	totr, err := charset.TranslatorTo(test.charset)
	if err != nil {
		t.Fatalf("error making translator to %q: %v", test.charset, err)
	}
	in, err := translate(totr, out)
	if err != nil {
		t.Fatalf("error translating to %q: %v", test.charset, err)
	}
	if in != test.in {
		t.Fatalf("%q round trip conversion failed; expected %x got %x", test.charset, test.in, in)
	}
}
Example #2
0
func listCharsets(verbose bool, csname string) {
	var buf bytes.Buffer
	if !verbose {
		if csname != "" {
			cs := charset.Info(csname)
			if cs == nil {
				fatalf("no such charset %q", csname)
			}
			fmt.Fprintf(&buf, "%s %s\n", cs.Name, strings.Join(cs.Aliases, " "))
		} else {
			fmt.Fprintf(&buf, "%v\n", strings.Join(charset.Names(), " "))
		}
	} else {
		var charsets []*charset.Charset
		if csname != "" {
			cs := charset.Info(csname)
			if cs == nil {
				fatalf("no such charset %q", csname)
			}
			charsets = []*charset.Charset{cs}
		} else {
			for _, name := range charset.Names() {
				if cs := charset.Info(name); cs != nil {
					charsets = append(charsets, cs)
				}
			}
		}
		for _, cs := range charsets {
			fmt.Fprintf(&buf, "%s %s\n", cs.Name, strings.Join(cs.Aliases, " "))
			if cs.Desc != "" {
				fmt.Fprintf(&buf, "\t%s\n", cs.Desc)
			}
		}
	}
	os.Stdout.Write(buf.Bytes())
}