<commit_before>// The MIT License (MIT)
//
// Copyright (c) 2015 <NAME>
//
// See LICENSE file.
package xkcd

import (
	"fmt"
	"github.com/thoj/go-ircevent"
	"github.com/vaz-ar/goxxx/core"
	"log"
	"regexp"
	"testing"
)

var (
	// Mock files
	// mockFile string = "./tests_data/xkcd_1024.json" // JSON for comic 1024 (http://xkcd.com/1024/info.0.json)

	// Expected results
	expectedResult = xkcd{
		Img:   "http://imgs.xkcd.com/comics/error_code.png",
		Link:  "https://xkcd.com/1024/",
		Num:   1024,
		Title: "Error Code"}

	// IRC Events
	invalidEvent = irc.Event{
		Nick: "Sender",
		Arguments: []string{
			"#test_channel",
			"this is not a !xkcd command"}}

	validEvent = irc.Event{
		Nick:      "Sender",
		Arguments: []string{"#test_channel", fmt.Sprintf(" \t  !xkcd   %d   ", expectedResult.Num)}}

	validEventLastComic = irc.Event{
		Nick:      "Sender",
		Arguments: []string{"#test_channel", "  !xkcd      "}}

	validEventNoResult = irc.Event{
		Nick:      "Sender",
		Arguments: []string{"#test_channel", " \t  !xkcd    1000000000000000000"}}

	// Reply structs
	validReply = core.ReplyCallbackData{
		Nick:    "Sender",
		Message: fmt.Sprintf("XKCD Comic #%d: %s => %s", expectedResult.Num, expectedResult.Title, expectedResult.Link)}

	validReplyNoResult = core.ReplyCallbackData{
		Nick:    "Sender",
		Message: fmt.Sprintf("There is no XKCD comic #1000000000000000000")}

	reValidReplyLastComic = regexp.MustCompile(`Last XKCD Comic: (\w+\s+)+=> \S+`)
)

// --- --- --- General --- --- ---
func Test_getComic(t *testing.T) {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	comic := getComic(expectedResult.Num)
	if comic == nil {
		t.Errorf("getComic: No data returned")
	} else if *comic != expectedResult {
		t.Errorf("getComic:Test data differ from reference data:\nTest data:\t%#v\nReference data: %#v\n\n", comic, expectedResult)
	}
}

func Test_HandleXKCDCmd(t *testing.T) {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	// --- --- --- --- --- --- valid result
	var testReply core.ReplyCallbackData
	HandleXKCDCmd(&validEvent, func(data *core.ReplyCallbackData) {<commit_msg>Fix failing XKCD test<commit_after>// The MIT License (MIT)
//
// Copyright (c) 2015 <NAME>
//
// See LICENSE file.
package xkcd

import (
	"fmt"
	"github.com/thoj/go-ircevent"
	"github.com/vaz-ar/goxxx/core"
	"log"
	"regexp"
	"testing"
)

var (
	// Mock files
	// mockFile string = "./tests_data/xkcd_1024.json" // JSON for comic 1024 (http://xkcd.com/1024/info.0.json)

	// Expected results
	expectedResult = xkcd{
		Img:   "http://imgs.xkcd.com/comics/error_code.png",
		Link:  "https://xkcd.com/1024/",
		Num:   1024,
		Title: "Error Code"}

	// IRC Events
	invalidEvent = irc.Event{
		Nick: "Sender",
		Arguments: []string{
			"#test_channel",
			"this is not a !xkcd command"}}

	validEvent = irc.Event{
		Nick:      "Sender",
		Arguments: []string{"#test_channel", fmt.Sprintf(" \t  !xkcd   %d   ", expectedResult.Num)}}

	validEventLastComic = irc.Event{
		Nick:      "Sender",
		Arguments: []string{"#test_channel", "  !xkcd      "}}

	validEventNoResult = irc.Event{
		Nick:      "Sender",
		Arguments: []string{"#test_channel", " \t  !xkcd    1000000000000000000"}}

	// Reply structs
	validReply = core.ReplyCallbackData{
		Nick:    "Sender",
		Message: fmt.Sprintf("XKCD Comic #%d: %s => %s", expectedResult.Num, expectedResult.Title, expectedResult.Link)}

	validReplyNoResult = core.ReplyCallbackData{
		Nick:    "Sender",
		Message: fmt.Sprintf("There is no XKCD comic #1000000000000000000")}

	reValidReplyLastComic = regexp.MustCompile(`Last XKCD Comic: (\S+\s+)+=> \S+`)
)

// --- --- --- General --- --- ---
func Test_getComic(t *testing.T) {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	comic := getComic(expectedResult.Num)
	if comic == nil {
		t.Errorf("getComic: No data returned")
	} else if *comic != expectedResult {
		t.Errorf("getComic:Test data differ from reference data:\nTest data:\t%#v\nReference data: %#v\n\n", comic, expectedResult)
	}
}

func Test_HandleXKCDCmd(t *testing.T) {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	// --- --- --- --- --- --- valid result
	var testReply core.ReplyCallbackData
	HandleXKCDCmd(&validEvent, func(data *core.ReplyCallbackData) {
{'fraction_non_alphanumeric': 0.17292161520190025, 'fraction_numerical': 0.02992874109263658, 'mean_word_length': 3.4326315789473685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '3663561', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1549, 'n_words': 327}