<commit_before>// Copyright 2015 Yahoo Inc.
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.

package rdl

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

func expectJSON(test *testing.T, val interface{}, expected string) {
	j, err := json.Marshal(val)
	if err != nil {
		test.Errorf("Cannot JSON marshal: %v", val)
	}
	sj := string(j)
	if sj != expected {
		test.Errorf("Unexpected marshal'd value: %s", sj)
	}
	//	fmt.Println(val, "=>", sj)
}

type TimestampTestStruct struct {
	ID  UUID       `json:"id"`
	Ref *UUID      `json:"ref,omitempty"`
	T1  Timestamp  `json:"t1"`
	T2  *Timestamp `json:"t2,omitempty"` //optional requires pointer, though idiom is non-pointer or time.Time
	Sym Symbol     `json:"sym,omitempty"`
}

func (ts *TimestampTestStruct) String() string {
	b, e := json.Marshal(ts)
	if e != nil {
		return "<TimestampTestStruct>"
	}
	return string(b)
}

func TestTimestamp(test *testing.T) {
	t := time.Now().UTC()
	ts1 := NewTimestamp(t)
	if ts1.Validate() != nil {
		test.Errorf("Cannot create timestamp from time.Time")
	}
	ts1a := Timestamp{t}
	if ts1 != ts1a {
		test.Errorf("%v should be equal to %v", ts1, ts1a)
	}
	ts2 := TimestampNow()
	if ts2.Validate() != nil {
		test.Errorf("Cannot create timestamp from current time")
	}
	ts3, err := TimestampParse("2015-03-14T15:59:26.535Z")
	if err != nil {
		test.Errorf("Cannot parse timestamp: %v", err)
	}
	fmt.Println("ts3:", ts3)
	if ts3.Validate() != nil {
		test.Errorf("Cannot parse timestamp")
	}
	expectJSON(test, ts3, `"2015-03-14T15:59:26.535Z"`)
	var tt TimestampTestStruct
	tt.ID = ParseUUID("275a2ede-ca77-11e4-9b30-003ee1be85f9")
	tt.T1 = ts3
	expectJSON(test, tt, `{"id":"275a2ede-ca77-11e4-9b30-003ee1be85f9","t1":"2015-03-14T15:59:26.535Z"}`)
}

func TestUUID(test *testing.T) {
	//any UUID generator that produces a []byte will work, i.e.:
	//    import "google.com/p/go-uuid/uuid"
	//    u0 := NewUUID(uuid.NewUUID())
	//    fmt.Println("new time-based uuid:", u0)
	u1 := ParseUUID("275a2ede-ca77-11e4-9b30-003ee1be85f9")
	if u1 == nil {
		test.Errorf("Valid UUID did not parse: %s", u1)
	}
	for _, u := range []string{
		"",<commit_msg>Added test for recent change.<commit_after>// Copyright 2015 Yahoo Inc.
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.

package rdl

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

func expectJSON(test *testing.T, val interface{}, expected string) {
	j, err := json.Marshal(val)
	if err != nil {
		test.Errorf("Cannot JSON marshal: %v", val)
	}
	sj := string(j)
	if sj != expected {
		test.Errorf("Unexpected marshal'd value: %s", sj)
	}
	//	fmt.Println(val, "=>", sj)
}

type TimestampTestStruct struct {
	ID  UUID       `json:"id"`
	Ref *UUID      `json:"ref,omitempty"`
	T1  Timestamp  `json:"t1"`
	T2  *Timestamp `json:"t2,omitempty"` //optional requires pointer, though idiom is non-pointer or time.Time
	Sym Symbol     `json:"sym,omitempty"`
}

func (ts *TimestampTestStruct) String() string {
	b, e := json.Marshal(ts)
	if e != nil {
		return "<TimestampTestStruct>"
	}
	return string(b)
}

func TestTimestamp(test *testing.T) {
	t := time.Now().UTC()
	ts1 := NewTimestamp(t)
	if ts1.Validate() != nil {
		test.Errorf("Cannot create timestamp from time.Time")
	}
	ts1a := Timestamp{t}
	if ts1 != ts1a {
		test.Errorf("%v should be equal to %v", ts1, ts1a)
	}
	ts2 := TimestampNow()
	if ts2.Validate() != nil {
		test.Errorf("Cannot create timestamp from current time")
	}
	ts3, err := TimestampParse("2015-03-14T15:59:26.535Z")
	if err != nil {
		test.Errorf("Cannot parse timestamp: %v", err)
	}
	fmt.Println("ts3:", ts3)
	if ts3.Validate() != nil {
		test.Errorf("Cannot parse timestamp")
	}
	ts4, err := TimestampParse("2015-03-14T15:59:26.535+00:00")
	if err != nil {
		test.Errorf("Cannot parse timestamp: %v", err)
	}
	fmt.Println("ts4:", ts4)
	if ts3.Validate() != nil {
		test.Errorf("Cannot parse timestamp")
	}
	expectJSON(test, ts4, `"2015-03-14T15:59:26.535Z"`)
	var tt TimestampTestStruct
	tt.ID = ParseUUID("275a2ede-ca77-11e4-9b30-003ee1be85f9")
	tt.T1 = ts3
	expectJSON(test, tt, `{"id":"275a2ede-ca77-11e4-9b30-003ee1be85f9","t1":"2015-03-14T15:59:26.535Z"}`)
}

func TestUUID(test *testing.T) {
	//any UUID generator that produces a []byte will work, i.e.:
	//    import "google.com/p/go-uuid/uuid"
	//    u0 := NewUUID(uuid.NewUUID())
	//    fmt.Println("new time-based uuid:", u0)
	u1 := ParseUUID("275a2ede-ca77-11e4-9b30-003ee1be85f9")
	if u1 == nil {
		test.Errorf("Valid UUID did not parse: %s", u1)
	}
	for _, u := range []string{
		"",
{'fraction_non_alphanumeric': 0.17139147370698982, 'fraction_numerical': 0.0660030296472625, 'mean_word_length': 3.880675818373812, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 7, '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': '7176715', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1832, 'n_words': 439}