...

Source file src/github.com/prometheus/procfs/internal/util/valueparser_test.go

Documentation: github.com/prometheus/procfs/internal/util

     1  // Copyright 2019 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package util_test
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/google/go-cmp/cmp"
    20  	"github.com/prometheus/procfs/internal/util"
    21  )
    22  
    23  func TestValueParser(t *testing.T) {
    24  	tests := []struct {
    25  		name string
    26  		v    string
    27  		ok   bool
    28  		fn   func(t *testing.T, vp *util.ValueParser)
    29  	}{
    30  		{
    31  			name: "ok Int",
    32  			v:    "10",
    33  			ok:   true,
    34  			fn: func(t *testing.T, vp *util.ValueParser) {
    35  				want := 10
    36  				got := vp.Int()
    37  
    38  				if diff := cmp.Diff(want, got); diff != "" {
    39  					t.Fatalf("unexpected integer (-want +got):\n%s", diff)
    40  				}
    41  			},
    42  		},
    43  		{
    44  			name: "bad PInt64",
    45  			v:    "hello",
    46  			fn: func(_ *testing.T, vp *util.ValueParser) {
    47  				_ = vp.PInt64()
    48  			},
    49  		},
    50  		{
    51  			name: "bad hex PInt64",
    52  			v:    "0xhello",
    53  			fn: func(_ *testing.T, vp *util.ValueParser) {
    54  				_ = vp.PInt64()
    55  			},
    56  		},
    57  		{
    58  			name: "ok PInt64",
    59  			v:    "1",
    60  			ok:   true,
    61  			fn: func(t *testing.T, vp *util.ValueParser) {
    62  				want := int64(1)
    63  				got := vp.PInt64()
    64  
    65  				if diff := cmp.Diff(&want, got); diff != "" {
    66  					t.Fatalf("unexpected integer (-want +got):\n%s", diff)
    67  				}
    68  			},
    69  		},
    70  		{
    71  			name: "ok hex PInt64",
    72  			v:    "0xff",
    73  			ok:   true,
    74  			fn: func(t *testing.T, vp *util.ValueParser) {
    75  				want := int64(255)
    76  				got := vp.PInt64()
    77  
    78  				if diff := cmp.Diff(&want, got); diff != "" {
    79  					t.Fatalf("unexpected integer (-want +got):\n%s", diff)
    80  				}
    81  			},
    82  		},
    83  		{
    84  			name: "bad PUInt64",
    85  			v:    "-42",
    86  			fn: func(_ *testing.T, vp *util.ValueParser) {
    87  				_ = vp.PUInt64()
    88  			},
    89  		},
    90  		{
    91  			name: "bad hex PUInt64",
    92  			v:    "0xhello",
    93  			fn: func(_ *testing.T, vp *util.ValueParser) {
    94  				_ = vp.PUInt64()
    95  			},
    96  		},
    97  		{
    98  			name: "ok PUInt64",
    99  			v:    "1",
   100  			ok:   true,
   101  			fn: func(t *testing.T, vp *util.ValueParser) {
   102  				want := uint64(1)
   103  				got := vp.PUInt64()
   104  
   105  				if diff := cmp.Diff(&want, got); diff != "" {
   106  					t.Fatalf("unexpected integer (-want +got):\n%s", diff)
   107  				}
   108  			},
   109  		},
   110  		{
   111  			name: "ok hex PUInt64",
   112  			v:    "0xff",
   113  			ok:   true,
   114  			fn: func(t *testing.T, vp *util.ValueParser) {
   115  				want := uint64(255)
   116  				got := vp.PUInt64()
   117  
   118  				if diff := cmp.Diff(&want, got); diff != "" {
   119  					t.Fatalf("unexpected integer (-want +got):\n%s", diff)
   120  				}
   121  			},
   122  		},
   123  	}
   124  
   125  	for _, tt := range tests {
   126  		t.Run(tt.name, func(t *testing.T) {
   127  			vp := util.NewValueParser(tt.v)
   128  			tt.fn(t, vp)
   129  
   130  			err := vp.Err()
   131  			if err != nil {
   132  				if tt.ok {
   133  					t.Fatalf("unexpected error: %v", err)
   134  				}
   135  
   136  				t.Logf("OK err: %v", err)
   137  				return
   138  			}
   139  
   140  			if !tt.ok {
   141  				t.Fatal("expected an error, but none occurred")
   142  			}
   143  		})
   144  	}
   145  }
   146  

View as plain text