<commit_before>// Copyright 2016 <NAME>. All rights reserved
// Released under the MIT license found in the LICENSE file.

package resource

import (
	"encoding/json" // because we explicitly want to work with the standard library json package
	"reflect"
	"strings"
	"testing"
)

func TestStatusString(t *testing.T) {
	type stringTest struct {
		Expected string
		Status   Status
	}
	tests := []stringTest{
		stringTest{ // Zero Value
			Expected: "0",
		},
		stringTest{ // Free
			Expected: "0",
			Status:   Free,
		},
		stringTest{ // Busy
			Expected: "1",
			Status:   Busy,
		},
		stringTest{ // Occupied
			Expected: "2",
			Status:   Occupied,
		},
		stringTest{ // Out of Range
			Expected: "0",
			Status:   Occupied + 1,
		},
	}
	for _, st := range tests {
		if actual := st.Status.String(); actual != st.Expected {
			t.Error("\nexpected:\t", st.Expected, "\n  actual:\t", actual)
		}
	}
}

func TestStatusPretty(t *testing.T) {
	type stringTest struct {
		Expected string
		Status   Status
	}
	tests := []stringTest{
		stringTest{ // Zero Value
			Expected: "Free",
		},
		stringTest{ // Free
			Expected: "Free",
			Status:   Free,
		},
		stringTest{ // Busy
			Expected: "Busy",
			Status:   Busy,
		},
		stringTest{ // Occupied
			Expected: "Occupied",
			Status:   Occupied,
		},
		stringTest{ // Out of Range
			Expected: "Free",
			Status:   Occupied + 1,
		},
	}
	for _, st := range tests {
		if actual := st.Status.Pretty(); actual != st.Expected {
			t.Error("\nexpected:\t", st.Expected, "\n  actual:\t", actual)
		}
	}
}

func TestStatusMarshalJSON(t *testing.T) {
	type testResponse struct {
		Value []byte
		Err   error
	}
	type jsonTest struct {
		Input    Status
		Expected testResponse
	}
	tests := []jsonTest{
		jsonTest{ // Zero Value
			Expected: testResponse{[]byte("0"), nil},
		},
		jsonTest{ // Free
			Input:    Free,
			Expected: testResponse{[]byte("0"), nil},
		},
		jsonTest{ // Busy
			Input:    Busy,
			Expected: testResponse{[]byte("1"), nil},
		},
		jsonTest{ // Occupied
			Input:    Occupied,
			Expected: testResponse{[]byte("2"), nil},
		},
		jsonTest{ // Out of Range
			Input:    Occupied + 1,
			Expected: testResponse{[]byte(""), ErrOutOfRange},
		},
	}
	for _, st := range tests {
		if actual, err := json.Marshal(st.Input); !rootError(err, st.Expected.Err) || string(actual) != string(st.Expected.Value) {
			t.Errorf("\nexpected:\t%q\t%q\n  actual:\t%q\t%q", string(st.Expected.Value), st.Expected.Err, string(actual), err)
		}
	}
}

// compare two errors to see if they are both nil or e contains root
func rootError(e, root error) bool {
	if e == nil && root == nil {
		return true
	} else if e == nil || root == nil {
		return false
	}
	return strings.Contains(e.Error(), root.Error())
}

func TestStatusUnmarshalJSON(t *testing.T) {
	type testResponse struct {
		Value Status
		ErrOK func(error) bool
	}
	type jsonTest struct {
		Input    []byte
		Expected testResponse
	}
	tests := []jsonTest{
		jsonTest{ // Free
			Input: []byte("0"),
			Expected: testResponse{
				Free,
				func(e error) bool { return e == nil },
			},
		},
		jsonTest{ // Busy
			Input: []byte("1"),
			Expected: testResponse{
				Busy,
				func(e error) bool { return e == nil },
			},
		},
		jsonTest{ // Occupied
			Input: []byte("2"),
			Expected: testResponse{
				Occupied,
				func(e error) bool { return e == nil },
			},
		},
		jsonTest{ // Out of Range
			Input: []byte("3"),
			Expected: testResponse{
				0,
				func(e error) bool { return e == ErrOutOfRange },
			},
		},
		jsonTest{ // Non-number
			Input: []byte("π"),
			Expected: testResponse{
				0,
				func(e error) bool { return e != nil },
			},
		},
	}
	for _, st := range tests {
		var actual Status
		err := json.Unmarshal(st.Input, &actual)
		if !st.Expected.ErrOK(err) {
			t.Errorf("Status.UnmarshalJSON(%v) = %v; expected: %#v", st.Input, err, st.Expected.ErrOK)
		}
		if !reflect.DeepEqual(actual, st.Expected.Value) {
			t.Errorf("Status.UnmarshalJSON(%v), Status: %v; expected: %v", st.Input, actual, st.Expected.Value)
		}
	}
}
<commit_msg>Updates Status test for well-formed non-numbers<commit_after>// Copyright 2016 <NAME>. All rights reserved
// Released under the MIT license found in the LICENSE file.

package resource

import (
	"encoding/json" // because we explicitly want to work with the standard library json package
	"reflect"
	"strings"
	"testing"
)

func TestStatusString(t *testing.T) {
	type stringTest struct {
		Expected string
		Status   Status
	}
	tests := []stringTest{
		stringTest{ // Zero Value
			Expected: "0",
		},
		stringTest{ // Free
			Expected: "0",
			Status:   Free,
		},
		stringTest{ // Busy
			Expected: "1",
			Status:   Busy,
		},
		stringTest{ // Occupied
			Expected: "2",
			Status:   Occupied,
		},
		stringTest{ // Out of Range
			Expected: "0",
			Status:   Occupied + 1,
		},
	}
	for _, st := range tests {
		if actual := st.Status.String(); actual != st.Expected {
			t.Error("\nexpected:\t", st.Expected, "\n  actual:\t", actual)
		}
	}
}

func TestStatusPretty(t *testing.T) {
	type stringTest struct {
		Expected string
		Status   Status
	}
	tests := []stringTest{
		stringTest{ // Zero Value
			Expected: "Free",
		},
		stringTest{ // Free
			Expected: "Free",
			Status:   Free,
		},
		stringTest{ // Busy
			Expected: "Busy",
			Status:   Busy,
		},
		stringTest{ // Occupied
			Expected: "Occupied",
			Status:   Occupied,
		},
		stringTest{ // Out of Range
			Expected: "Free",
			Status:   Occupied + 1,
		},
	}
	for _, st := range tests {
		if actual := st.Status.Pretty(); actual != st.Expected {
			t.Error("\nexpected:\t", st.Expected, "\n  actual:\t", actual)
		}
	}
}

func TestStatusMarshalJSON(t *testing.T) {
	type testResponse struct {
		Value []byte
		Err   error
	}
	type jsonTest struct {
		Input    Status
		Expected testResponse
	}
	tests := []jsonTest{
		jsonTest{ // Zero Value
			Expected: testResponse{[]byte("0"), nil},
		},
		jsonTest{ // Free
			Input:    Free,
			Expected: testResponse{[]byte("0"), nil},
		},
		jsonTest{ // Busy
			Input:    Busy,
			Expected: testResponse{[]byte("1"), nil},
		},
		jsonTest{ // Occupied
			Input:    Occupied,
			Expected: testResponse{[]byte("2"), nil},
		},
		jsonTest{ // Out of Range
			Input:    Occupied + 1,
			Expected: testResponse{[]byte(""), ErrOutOfRange},
		},
	}
	for _, st := range tests {
		if actual, err := json.Marshal(st.Input); !rootError(err, st.Expected.Err) || string(actual) != string(st.Expected.Value) {
			t.Errorf("\nexpected:\t%q\t%q\n  actual:\t%q\t%q", string(st.Expected.Value), st.Expected.Err, string(actual), err)
		}
	}
}

// compare two errors to see if they are both nil or e contains root
func rootError(e, root error) bool {
	if e == nil && root == nil {
		return true
	} else if e == nil || root == nil {
		return false
	}
	return strings.Contains(e.Error(), root.Error())
}

func TestStatusUnmarshalJSON(t *testing.T) {
	type testResponse struct {
		Value Status
		ErrOK func(error) bool
	}
	type jsonTest struct {
		Input    []byte
		Expected testResponse
	}
	tests := []jsonTest{
		jsonTest{ // Free
			Input: []byte("0"),
			Expected: testResponse{
				Free,
				func(e error) bool { return e == nil },
			},
		},
		jsonTest{ // Busy
			Input: []byte("1"),
			Expected: testResponse{
				Busy,
				func(e error) bool { return e == nil },
			},
		},
		jsonTest{ // Occupied
			Input: []byte("2"),
			Expected: testResponse{
				Occupied,
				func(e error) bool { return e == nil },
			},
		},
		jsonTest{ // Out of Range
			Input: []byte("3"),
			Expected: testResponse{
				0,
				func(e error) bool { return e == ErrOutOfRange },
			},
		},
		jsonTest{ // Non-number
			Input: []byte(`"π"`),
			Expected: testResponse{
				0,
				func(e error) bool { return e != nil },
			},
		},
	}
	for _, st := range tests {
		var actual Status
		err := json.Unmarshal(st.Input, &actual)
		if !st.Expected.ErrOK(err) {
			t.Errorf("Status.UnmarshalJSON(%v) = %v; expected: %#v", st.Input, err, st.Expected.ErrOK)
		}
		if !reflect.DeepEqual(actual, st.Expected.Value) {
			t.Errorf("Status.UnmarshalJSON(%v), Status: %v; expected: %v", st.Input, actual, st.Expected.Value)
		}
	}
}

{'fraction_non_alphanumeric': 0.1587340529931305, 'fraction_numerical': 0.005397448478900883, 'mean_word_length': 2.931051108968177, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5413504', 'n_tokens_mistral': 3250, 'n_tokens_neox': 2828, 'n_words': 765}