<commit_before>// Copyright 2019 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util_test

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/prometheus/procfs/internal/util"
)

func TestValueParser(t *testing.T) {
	tests := []struct {
		name string
		v    string
		ok   bool
		fn   func(t *testing.T, vp *util.ValueParser)
	}{
		{
			name: "bad PInt64",
			v:    "hello",
			fn: func(_ *testing.T, vp *util.ValueParser) {
				_ = vp.PInt64()
			},
		},
		{
			name: "bad hex PInt64",
			v:    "0xhello",
			fn: func(_ *testing.T, vp *util.ValueParser) {
				_ = vp.PInt64()
			},
		},
		{
			name: "ok PInt64",
			v:    "1",
			ok:   true,
			fn: func(t *testing.T, vp *util.ValueParser) {
				want := int64(1)
				got := vp.PInt64()

				if diff := cmp.Diff(&want, got); diff != "" {
					t.Fatalf("unexpected integer (-want +got):\n%s", diff)
				}
			},
		},
		{
			name: "ok hex PInt64",
			v:    "0xff",
			ok:   true,
			fn: func(t *testing.T, vp *util.ValueParser) {
				want := int64(255)
				got := vp.PInt64()

				if diff := cmp.Diff(&want, got); diff != "" {
					t.Fatalf("unexpected integer (-want +got):\n%s", diff)
				}
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			vp := util.NewValueParser(tt.v)
			tt.fn(t, vp)

			err := vp.Err()
			if err != nil {
				if tt.ok {
					t.Fatalf("unexpected error: %v", err)
				}

				t.Logf("OK err: %v", err)
				return
			}

			if err == nil && !tt.ok {
				t.Fatal("expected an error, but none occurred")
			}
		})
	}
}<commit_msg>internal/util: remove redundant error check<commit_after>// Copyright 2019 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util_test

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/prometheus/procfs/internal/util"
)

func TestValueParser(t *testing.T) {
	tests := []struct {
		name string
		v    string
		ok   bool
		fn   func(t *testing.T, vp *util.ValueParser)
	}{
		{
			name: "bad PInt64",
			v:    "hello",
			fn: func(_ *testing.T, vp *util.ValueParser) {
				_ = vp.PInt64()
			},
		},
		{
			name: "bad hex PInt64",
			v:    "0xhello",
			fn: func(_ *testing.T, vp *util.ValueParser) {
				_ = vp.PInt64()
			},
		},
		{
			name: "ok PInt64",
			v:    "1",
			ok:   true,
			fn: func(t *testing.T, vp *util.ValueParser) {
				want := int64(1)
				got := vp.PInt64()

				if diff := cmp.Diff(&want, got); diff != "" {
					t.Fatalf("unexpected integer (-want +got):\n%s", diff)
				}
			},
		},
		{
			name: "ok hex PInt64",
			v:    "0xff",
			ok:   true,
			fn: func(t *testing.T, vp *util.ValueParser) {
				want := int64(255)
				got := vp.PInt64()

				if diff := cmp.Diff(&want, got); diff != "" {
					t.Fatalf("unexpected integer (-want +got):\n%s", diff)
				}
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			vp := util.NewValueParser(tt.v)
			tt.fn(t, vp)

			err := vp.Err()
			if err != nil {
				if tt.ok {
					t.Fatalf("unexpected error: %v", err)
				}

				t.Logf("OK err: %v", err)
				return
			}

			if !tt.ok {
				t.Fatal("expected an error, but none occurred")
			}
		})
	}
}
{'fraction_non_alphanumeric': 0.15764254911356013, 'fraction_numerical': 0.016770483948251078, 'mean_word_length': 2.544142614601019, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2342994', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1528, 'n_words': 440}