Codebase list golang-github-ianlancetaylor-demangle / run/bb39a423-4c17-4802-9281-ec5fe01e6a84/upstream rust_expected_test.go
run/bb39a423-4c17-4802-9281-ec5fe01e6a84/upstream

Tree @run/bb39a423-4c17-4802-9281-ec5fe01e6a84/upstream (Download .tar.gz)

rust_expected_test.go @run/bb39a423-4c17-4802-9281-ec5fe01e6a84/upstreamraw · history · blame

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package demangle

import (
	"bufio"
	"fmt"
	"os"
	"strings"
	"testing"
)

const rustFilename = "testdata/rust-demangle-expected"

// TestRustExpected is like TestExpected, but for Rust demangling.
// We ignore all test inputs that do not start with _R.
func TestRustExpected(t *testing.T) {
	t.Parallel()
	f, err := os.Open(rustFilename)
	if err != nil {
		t.Fatal(err)
	}
	scanner := bufio.NewScanner(f)
	lineno := 1
	for {
		format, got := getOptLine(t, scanner, &lineno)
		if !got {
			break
		}
		report := lineno
		input := getLine(t, scanner, &lineno)
		expect := getLine(t, scanner, &lineno)

		skip := false
		if len(format) > 0 && format[0] == '-' {
			for _, arg := range strings.Fields(format) {
				switch arg {
				case "--format=gnu-v3":
					skip = true
				case "--format=auto":
				case "--format=rust":
				default:
					t.Errorf("%s:%d: unrecognized argument %s", rustFilename, report, arg)
				}
			}
		}

		if skip {
			continue
		}

		oneRustTest(t, report, input, expect)
	}
	if err := scanner.Err(); err != nil {
		t.Error(err)
	}
}

// oneRustTest tests one entry from rust-demangle-expected.
func oneRustTest(t *testing.T, report int, input, expect string) {
	if *verbose {
		fmt.Println(input)
	}

	s, err := ToString(input)
	if err != nil {
		if err != ErrNotMangledName {
			if input == expect {
				return
			}
			t.Errorf("%s:%d: %v", rustFilename, report, err)
			return
		}
		s = input
	}

	if s != expect {
		t.Errorf("%s:%d: got %q, want %q", rustFilename, report, s, expect)
	}
}

const rustCheckFilename = "testdata/rust.test"

func TestRustCheck(t *testing.T) {
	t.Parallel()
	f, err := os.Open(rustCheckFilename)
	if err != nil {
		t.Fatal(err)
	}
	scanner := bufio.NewScanner(f)
	lineno := 1
	for scanner.Scan() {
		report := lineno
		line := strings.TrimSpace(scanner.Text())
		lineno++
		if !strings.HasPrefix(line, "CHECK: ") {
			continue
		}
		want := strings.TrimPrefix(line, "CHECK: ")
		if !scanner.Scan() {
			t.Fatalf("%s:%d: unexpected EOF", rustCheckFilename, report)
		}
		lineno++
		input := strings.TrimSpace(scanner.Text())

		got, err := ToString(input, LLVMStyle)
		if err != nil {
			if want != input {
				t.Errorf("%s:%d: %v", rustCheckFilename, report, err)
			}
		} else if got != want {
			t.Errorf("%s:%d: got %q, want %q", rustCheckFilename, report, got, want)
		}
	}
	if err := scanner.Err(); err != nil {
		t.Error(err)
	}
}