<commit_before>package gosnow

import (
	"encoding/json"
	"io/ioutil"
	"testing"
)

const simpleSrc string = `
# My API
## GET /message
 + Response 200 (text/plain)

         Hello World
`

const namelessSrc string = `
FORMAT: 1A

# Group Messages

# Message [/messages/{id}]

## Retrieve Message [GET]
+ Response 200 (text/plain)

        Hello World!

`

const warningSrc string = `
FORMAT: 1A

# Group Messages

# Message [/messages/{id}]

+ Model (text/plain)

        Hello World

## Retrieve Message [GET]
+ Response 200 (text/plain)

        Hello World!

## Retrieve Message [GET]
+ Response 200 (text/plain)

        Hello World!

`

var (
	apibFile      = "test/fixtures/sample-api.apib"
	astFile       = "test/fixtures/sample-api-ast.json"
	sourcemapFile = "test/fixtures/sample-api-sourcemap.json"
)

// replace the variables with the contents of the file they point to
func init() {
	if c, err := ioutil.ReadFile(apibFile); err != nil {
		panic("apibFile not found")
	} else {
		apibFile = string(c)
	}

	if c, err := ioutil.ReadFile(astFile); err != nil {
		panic("astFile not found")
	} else {
		astFile = string(c)
	}

	if c, err := ioutil.ReadFile(sourcemapFile); err != nil {
		panic("sourcemapFile not found")
	} else {
		sourcemapFile = string(c)
	}
}

func TestParse(t *testing.T) {
	res, err := Parse(apibFile)
	if err != nil {
		t.Fatalf("Parse failed with error: %v", err)
	}
	if res == nil {
		t.Fatal("Parse returned nil result")
	}
	// v, _ := json.MarshalIndent(res, "", "  ")
	// fmt.Println(string(v))
}

// ensure that the option parse with a 0 does the same thing as the simple parse
func TestParseEquality(t *testing.T) {
	res1, err := Parse(simpleSrc)
	if err != nil {
		t.Fatalf("Parse failed with err: %v", err)
	}
	res2, err := OptionParse(simpleSrc, 0)
	if err != nil {
		t.Fatalf("OptionParse failed with err: %v", err)
	}<commit_msg>added some tests for new parser methods<commit_after>package gosnow

import (
	"encoding/json"
	"io/ioutil"
	"testing"
)

const simpleSrc string = `
# My API
## GET /message
 + Response 200 (text/plain)

         Hello World
`

const namelessSrc string = `
FORMAT: 1A

# Group Messages

# Message [/messages/{id}]

## Retrieve Message [GET]
+ Response 200 (text/plain)

        Hello World!

`

const warningSrc string = `
FORMAT: 1A

# Group Messages

# Message [/messages/{id}]

+ Model (text/plain)

        Hello World

## Retrieve Message [GET]
+ Response 200 (text/plain)

        Hello World!

## Retrieve Message [GET]
+ Response 200 (text/plain)

        Hello World!

`

var (
	apibFile      = "test/fixtures/sample-api.apib"
	astFile       = "test/fixtures/sample-api-ast.json"
	sourcemapFile = "test/fixtures/sample-api-sourcemap.json"
)

// replace the variables with the contents of the file they point to
func init() {
	if c, err := ioutil.ReadFile(apibFile); err != nil {
		panic("apibFile not found")
	} else {
		apibFile = string(c)
	}

	if c, err := ioutil.ReadFile(astFile); err != nil {
		panic("astFile not found")
	} else {
		astFile = string(c)
	}

	if c, err := ioutil.ReadFile(sourcemapFile); err != nil {
		panic("sourcemapFile not found")
	} else {
		sourcemapFile = string(c)
	}
}

func TestSourceAnnotationOk(t *testing.T) {
	sa := new(SourceAnnotation)
	if !sa.Ok() {
		t.Error("empty source annotation should have zero value indicating ok")
	}
	sa.Code = 2
	if sa.Ok() {
		t.Error("source annotation should have non zero value indicating not ok")
	}
}

func TestNewPR(t *testing.T) {
	_, err := newPR([]byte(`{"unrelated": "json"}`))
	if err != nil {
		t.Errorf("newPR errored for valid json %v", err)
	}
}

func TestNewPRFailure(t *testing.T) {
	junk := []byte(`*#(*(R$#&)$#)R*(Y@#_RH`)
	_, err := newPR(junk)
	if err == nil {
		t.Error("newPR should have errored and did not")
	}
	if e, ok := err.(*json.SyntaxError); !ok {
		t.Errorf("Expected json.SyntaxError, got %T", e)
	}
}

func TestParse(t *testing.T) {
	res, err := Parse(apibFile)
	if err != nil {
		t.Fatalf("Parse failed with error: %v", err)
	}
	if res == nil {
		t.Fatal("Parse returned nil result")
	}
	// v, _ := json.MarshalIndent(res, "", "  ")
	// fmt.Println(string(v))
}

// ensure that the option parse with a 0 does the same thing as the simple parse
func TestParseEquality(t *testing.T) {
	res1, err := Parse(simpleSrc)
	if err != nil {
		t.Fatalf("Parse failed with err: %v", err)
	}
	res2, err := OptionParse(simpleSrc, 0)
	if err != nil {
		t.Fatalf("OptionParse failed with err: %v", err)
	}
{'fraction_non_alphanumeric': 0.14812332439678283, 'fraction_numerical': 0.008266309204647005, 'mean_word_length': 3.346601941747573, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, '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': '2075972', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1627, 'n_words': 459}