Skip to content

go test: idea: f vs anonymous struct #22

@AmitKumarDas

Description

@AmitKumarDas
package logstorage

import (
  "testing"
)

func TestLogfmtParser(t *testing.T) {
  f := func(given, want string) {
    t.Helper() // to get the test case's exact loc
  
    p := getLogfmtParser()
    defer putLogfmtParser(p)
    
    p.parse(given)
    got := MarshalFieldsToJSON(nil, p.fields)
    if string(got) != want {
      t.Errorf("MarshalFieldsToJSON [%s]:\ngot:%s\nwant:%s\n", given, got, want)
    }
  }
  
  // f(given, want)
  f(``, `{}`)
  f(`foo=bar`, `{"foo":"bar"}`)
  f(`foo="bar=baz x=y"`, `{"foo":"bar=baz x=y"}`)
  f(`foo=`, `{"foo":""}`)
  f(`foo`, `{"foo":""}`)
  f(`foo bar`, `{"foo":"","bar":""}`)
  f(`foo bar=baz`, `{"foo":"","bar":"baz"}`)
  f(`foo=bar baz="x y" a=b`, `{"foo":"bar","baz":"x y","a":"b"}`)
  f(`  foo=bar  baz=x =z qwe`, `{"foo":"bar","baz":"x","_msg":"z","qwe":""}`)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions