github.com/apcera/util@v0.0.0-20180322191801-7a50bc84ee48/proc/parser_test.go (about)

     1  // Copyright 2013 Apcera Inc. All rights reserved.
     2  
     3  package proc
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  
    10  	tt "github.com/apcera/util/testtool"
    11  )
    12  
    13  func TestReadInt64(t *testing.T) {
    14  	testHelper := tt.StartTest(t)
    15  	defer testHelper.FinishTest()
    16  
    17  	f := testHelper.WriteTempFile("foo\nbar")
    18  
    19  	_, err := ReadInt64(f)
    20  	tt.TestExpectError(t, err)
    21  
    22  	f = testHelper.WriteTempFile("123\n456")
    23  
    24  	v, err := ReadInt64(f)
    25  	tt.TestExpectSuccess(t, err)
    26  	tt.TestEqual(t, v, int64(123))
    27  
    28  	f = testHelper.WriteTempFile("123456789")
    29  	v, err = ReadInt64(f)
    30  	tt.TestExpectSuccess(t, err)
    31  	tt.TestEqual(t, v, int64(123456789))
    32  
    33  	maxInt64 := fmt.Sprintf("%d", int64(1<<63-1))
    34  	f = testHelper.WriteTempFile(maxInt64)
    35  
    36  	v, err = ReadInt64(f)
    37  	tt.TestExpectSuccess(t, err)
    38  	tt.TestEqual(t, v, int64(1<<63-1))
    39  
    40  	maxInt64WithExtra := fmt.Sprintf("%d666", int64(1<<63-1))
    41  	f = testHelper.WriteTempFile(maxInt64WithExtra)
    42  
    43  	v, err = ReadInt64(f)
    44  	tt.TestExpectSuccess(t, err)
    45  	tt.TestEqual(t, v, int64(1<<63-1))
    46  }
    47  
    48  func TestParseSimpleProcFile(t *testing.T) {
    49  	testHelper := tt.StartTest(t)
    50  	defer testHelper.FinishTest()
    51  
    52  	// Test 1: Success.
    53  	lines := []string{
    54  		"aelm0 aelm1\taelm2",
    55  		" belm0  belm1\t belm2\t\t\t",
    56  		"",
    57  		"delm0"}
    58  	f := testHelper.WriteTempFile(strings.Join(lines, "\n"))
    59  	err := ParseSimpleProcFile(
    60  		f,
    61  		func(index int, line string) error {
    62  			if index > len(lines) {
    63  				tt.Fatalf(t, "Too many lines read: %d", index)
    64  			} else if line != lines[index] {
    65  				tt.Fatalf(t, "Invalid line read: %s", line)
    66  			}
    67  			return nil
    68  		},
    69  		func(line int, index int, elm string) error {
    70  			switch {
    71  			case line == 0 && index == 0 && elm == "aelm0":
    72  			case line == 0 && index == 1 && elm == "aelm1":
    73  			case line == 0 && index == 2 && elm == "aelm2":
    74  			case line == 1 && index == 0 && elm == "belm0":
    75  			case line == 1 && index == 1 && elm == "belm1":
    76  			case line == 1 && index == 2 && elm == "belm2":
    77  			case line == 3 && index == 0 && elm == "delm0":
    78  			default:
    79  				tt.Fatalf(
    80  					t, "Unknown element read: %d, %d, %s", line, index, elm)
    81  			}
    82  			return nil
    83  		})
    84  	if err != nil {
    85  		tt.Fatalf(t, "Unexpected error from ParseSimpleProcFile()")
    86  	}
    87  
    88  	// Test 2: No function defined. This should be successful.
    89  	err = ParseSimpleProcFile(f, nil, nil)
    90  	if err != nil {
    91  		tt.Fatalf(t, "Unexpected error from ParseSimpleProcFile()")
    92  	}
    93  
    94  	// Test 3: ef returns an error.
    95  	err = ParseSimpleProcFile(
    96  		f,
    97  		func(index int, line string) error {
    98  			return fmt.Errorf("error.")
    99  		},
   100  		nil)
   101  	if err == nil {
   102  		tt.Fatalf(t, "Expected error not returned.")
   103  	}
   104  
   105  	// Test 4: lf returns an error.
   106  	err = ParseSimpleProcFile(
   107  		f,
   108  		nil,
   109  		func(line int, index int, elm string) error {
   110  			return fmt.Errorf("error.")
   111  		})
   112  	if err == nil {
   113  		tt.Fatalf(t, "Expected error not returned.")
   114  	}
   115  
   116  	// Test 6: last case lf operation.
   117  	err = ParseSimpleProcFile(
   118  		f,
   119  		func(index int, line string) error {
   120  			if line == "delm0" {
   121  				return fmt.Errorf("error")
   122  			}
   123  			return nil
   124  		},
   125  		nil)
   126  
   127  	// Test 5: last case lf operation.
   128  	err = ParseSimpleProcFile(
   129  		f,
   130  		nil,
   131  		func(line int, index int, elm string) error {
   132  			if elm == "delm0" {
   133  				return fmt.Errorf("error")
   134  			}
   135  			return nil
   136  		})
   137  }