<commit_before>package ua

import (
	"fmt"
	"io/ioutil"
	"launchpad.net/goyaml"
	"testing"
)

func uaInitTesting(file string) []map[string]string {
	fmt.Print(file + ": ")
	testFile, _ := ioutil.ReadFile(file)
	testMap := make(map[string][]map[string]string)
	_ = goyaml.Unmarshal(testFile, &testMap)
	return testMap["test_cases"]
}

var uaDefaultRegexFile string = "../../regexes.yaml"
var uaParser *Parser = nil

func uaInitParser(regexFile string) {
	if uaParser == nil {
		uaParser = NewFromFile(regexFile)
	}
}

func uaInitParserNew(regexFile string) {

	if uaParser == nil {
		uaParser, err := NewParser(regexFile)
		if uaParser != nil {
			fmt.Println(err.Error())
		}

	}
}

func uaHelperTest(file string) bool {
	uaInitParserNew(uaDefaultRegexFile)
	tests := uaInitTesting(file)
	for _, test := range tests {

		// Other language ports of ua_parser skips js_ua in testing
		if test["js_ua"] != "" {
			continue
		}

		testingString := test["user_agent_string"]
		ua := uaParser.ParseUserAgent(testingString)

		if ua.Family != test["family"] || ua.Major != test["major"] ||
			ua.Minor != test["minor"] || ua.Patch != test["patch"] {
			fmt.Println("FAIL")
			fmt.Printf("Expected: %v\nActual: %v\n", test, ua)
			return false
		}
	}
	return true
}

func TestUserAgent(t *testing.T) {
	if !uaHelperTest("../../test_resources/test_user_agent_parser.yaml") {
		t.Fail()
	} else {
		fmt.Println("PASS")
	}<commit_msg>adding test<commit_after>package ua

import (
	"fmt"
	"io/ioutil"
	"launchpad.net/goyaml"
	"testing"
)

func uaInitTesting(file string) []map[string]string {
	fmt.Print(file + ": ")
	testFile, _ := ioutil.ReadFile(file)
	testMap := make(map[string][]map[string]string)
	_ = goyaml.Unmarshal(testFile, &testMap)
	return testMap["test_cases"]
}

var uaDefaultRegexFile string = "../../regexes.yaml"
var uaParser *Parser = nil

func uaInitParser(regexFile string) {
	if uaParser == nil {
		uaParser = NewFromFile(regexFile)
	}
}

func uaInitParserNew(regexFile string) {

	if uaParser == nil {
		uaParser, err := NewParser(regexFile)
		if uaParser != nil {
			fmt.Println(err.Error())
		}

	}
}

func uaHelperTest(file string) bool {
	uaInitParserNew(uaDefaultRegexFile)
	uaInitParser(uaDefaultRegexFile)
	tests := uaInitTesting(file)
	for _, test := range tests {

		// Other language ports of ua_parser skips js_ua in testing
		if test["js_ua"] != "" {
			continue
		}

		testingString := test["user_agent_string"]
		ua := uaParser.ParseUserAgent(testingString)

		if ua.Family != test["family"] || ua.Major != test["major"] ||
			ua.Minor != test["minor"] || ua.Patch != test["patch"] {
			fmt.Println("FAIL")
			fmt.Printf("Expected: %v\nActual: %v\n", test, ua)
			return false
		}
	}
	return true
}

func TestUserAgent(t *testing.T) {
	if !uaHelperTest("../../test_resources/test_user_agent_parser.yaml") {
		t.Fail()
	} else {
		fmt.Println("PASS")
	}
{'fraction_non_alphanumeric': 0.15705350938151494, 'fraction_numerical': 0.0, 'mean_word_length': 4.234545454545454, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5352121', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1075, 'n_words': 208}