github.com/zhangbo2008/go_cherry_room@v0.0.0-20200225122943-fc7e48f14105/src/pkg/config/parser/parser_test.go (about)

     1  /*
     2   *                          Copyright (C) 2015, 2016 by Rafael Santiago
     3   *
     4   * This is a free software. You can redistribute it and/or modify under
     5   * the terms of the GNU General Public License version 2.
     6   *
     7   */
     8  package parser
     9  
    10  import (
    11  	"cherry/src/pkg/config"
    12  	"fmt"
    13  	"os"
    14  	"testing"
    15  )
    16  
    17  func TestGetDataFromSection(t *testing.T) {
    18  	var configData = "# The commentary\n\n\n # More commentaries\ncherry.rooms ( aliens-on-earth:1024\nbackyard-science:911\n )\n"
    19  	secData, _, _, err := GetDataFromSection("cherry.room", configData, 1, "foobar.cherry")
    20  	if err == nil || len(secData) > 0 {
    21  		t.Fail()
    22  	}
    23  	secData, _, _, err = GetDataFromSection("cherry.rooms", configData, 1, "foobar.cherry")
    24  	if err != nil || secData != " aliens-on-earth:1024\nbackyard-science:911\n " {
    25  		t.Fail()
    26  	}
    27  }
    28  
    29  func TestStripBlanks(t *testing.T) {
    30  	var data = " \t\t\t   \t\t\t   o u t e r  s p a c e   \t\t\t \t \t  \t"
    31  	fmt.Println(StripBlanks(data))
    32  	if StripBlanks(data) != "o u t e r  s p a c e" {
    33  		t.Fail()
    34  	}
    35  }
    36  
    37  func TestGetNextSetFromData(t *testing.T) {
    38  	var data = "aliens-on-earth:1024\nbackyard-science : 911\n"
    39  	set, _, data := GetNextSetFromData(data, 1, ":")
    40  	if len(set) != 2 || len(data) == 0 || set[0] != "aliens-on-earth" || set[1] != "1024" {
    41  		t.Fail()
    42  	}
    43  	set, _, data = GetNextSetFromData(data, 1, ":")
    44  	if len(set) != 2 || len(data) == 0 || set[0] != "backyard-science" || set[1] != "911" {
    45  		t.Fail()
    46  	}
    47  	set, _, data = GetNextSetFromData(data, 1, ":")
    48  	if len(set) != 1 || len(data) != 0 {
    49  		t.Fail()
    50  	}
    51  	data = "i01 = \"http://www.nowhere.com/images/i01.gif\"\ni02 = \"http://www.nowhere.com/images/i02.gif\"\ni03 = \"http://www.nowhere.com/images/i03.gif\"\n"
    52  	set, _, data = GetNextSetFromData(data, 1, "=")
    53  	if len(set) != 2 || len(data) == 0 || set[0] != "i01" || set[1] != "\"http://www.nowhere.com/images/i01.gif\"" {
    54  		t.Fail()
    55  	}
    56  	set, _, data = GetNextSetFromData(data, 1, "=")
    57  	if len(set) != 2 || len(data) == 0 || set[0] != "i02" || set[1] != "\"http://www.nowhere.com/images/i02.gif\"" {
    58  		t.Fail()
    59  	}
    60  	set, _, data = GetNextSetFromData(data, 1, "=")
    61  	if len(set) != 2 || len(data) == 0 || set[0] != "i03" || set[1] != "\"http://www.nowhere.com/images/i03.gif\"" {
    62  		t.Fail()
    63  	}
    64  }
    65  
    66  func TestRealCherryFileParsing(t *testing.T) {
    67  	//  INFO(Santiago): This Homeric test should be splitted in the future.
    68  	var cherryRooms *config.CherryRooms
    69  	cwd, _ := os.Getwd()
    70  	os.Chdir("../../../../sample")
    71  	var error *CherryFileError
    72  	cherryRooms, error = ParseCherryFile("conf/sample.cherry")
    73  	os.Chdir(cwd)
    74  	if error != nil {
    75  		fmt.Println(error)
    76  		t.Fail()
    77  	}
    78  	if cherryRooms == nil {
    79  		t.Fail()
    80  	}
    81  	var rooms []string
    82  	rooms = cherryRooms.GetRooms()
    83  	if len(rooms) != 1 {
    84  		t.Fail()
    85  	}
    86  	if rooms[0] != "aliens-on-earth" {
    87  		t.Fail()
    88  	}
    89  	if cherryRooms.GetListenPort(rooms[0]) != "1024" {
    90  		t.Fail()
    91  	}
    92  	if cherryRooms.GetUsersTotal(rooms[0]) != "0" {
    93  		t.Fail()
    94  	}
    95  	if cherryRooms.GetGreetingMessage(rooms[0]) != "Take meeeeee to your leader!!!" {
    96  		t.Fail()
    97  	}
    98  	if cherryRooms.GetJoinMessage(rooms[0]) != "joined...<script>scrollIt();</script>" {
    99  		t.Fail()
   100  	}
   101  	if cherryRooms.GetExitMessage(rooms[0]) != "has left...<script>scrollIt();</script>" {
   102  		t.Fail()
   103  	}
   104  	if cherryRooms.GetOnIgnoreMessage(rooms[0]) != "(only you can see it) IGNORING " {
   105  		t.Fail()
   106  	}
   107  	if cherryRooms.GetOnDeIgnoreMessage(rooms[0]) != "(only you can see it) is NOT IGNORING " {
   108  		t.Fail()
   109  	}
   110  	if cherryRooms.GetPrivateMessageMarker(rooms[0]) != "(private)" {
   111  		t.Fail()
   112  	}
   113  	if cherryRooms.GetAllUsersAlias(rooms[0]) != "EVERYBODY" {
   114  		t.Fail()
   115  	}
   116  	if cherryRooms.GetMaxUsers(rooms[0]) != "10" {
   117  		t.Fail()
   118  	}
   119  	if !cherryRooms.IsAllowingBriefs(rooms[0]) {
   120  		t.Fail()
   121  	}
   122  	if len(cherryRooms.GetCertificatePath()) != 0 {
   123  		t.Fail()
   124  	}
   125  	cherryRooms.SetCertificatePath("foo.crt")
   126  	if cherryRooms.GetCertificatePath() != "foo.crt" {
   127  		t.Fail()
   128  	}
   129  	if len(cherryRooms.GetPrivateKeyPath()) != 0 {
   130  		t.Fail()
   131  	}
   132  	cherryRooms.SetPrivateKeyPath("foo.key")
   133  	if cherryRooms.GetPrivateKeyPath() != "foo.key" {
   134  		t.Fail()
   135  	}
   136  	var expActionLabels map[string]string
   137  	expActionLabels = make(map[string]string)
   138  	expActionLabels["a01"] = "talks to"
   139  	expActionLabels["a02"] = "screams with"
   140  	expActionLabels["a03"] = "IGNORE"
   141  	expActionLabels["a04"] = "NOT IGNORE"
   142  	for a, l := range expActionLabels {
   143  		if cherryRooms.GetRoomActionLabel(rooms[0], a) != l {
   144  			t.Fail()
   145  		}
   146  		if len(cherryRooms.GetRoomActionTemplate(rooms[0], a)) == 0 {
   147  			t.Fail()
   148  		}
   149  	}
   150  	if cherryRooms.GetUsersTotal(rooms[0]) != "0" {
   151  		t.Fail()
   152  	}
   153  	cherryRooms.AddUser(rooms[0], "dunha", "0", false)
   154  	if cherryRooms.GetUsersTotal(rooms[0]) != "1" {
   155  		t.Fail()
   156  	}
   157  	if len(cherryRooms.GetSessionID("dunha", rooms[0])) == 0 {
   158  		t.Fail()
   159  	}
   160  	if cherryRooms.GetColor("dunha", rooms[0]) != "0" {
   161  		t.Fail()
   162  	}
   163  	if len(cherryRooms.GetIgnoreList("dunha", rooms[0])) != 0 {
   164  		t.Fail()
   165  	}
   166  	cherryRooms.AddToIgnoreList("dunha", "quiet", rooms[0])
   167  	if cherryRooms.GetIgnoreList("dunha", rooms[0]) == "\"quiet\"" {
   168  		t.Fail()
   169  	}
   170  	cherryRooms.DelFromIgnoreList("dunha", "quiet", rooms[0])
   171  	if len(cherryRooms.GetIgnoreList("dunha", rooms[0])) != 0 {
   172  		t.Fail()
   173  	}
   174  	cherryRooms.RemoveUser(rooms[0], "donha")
   175  	if cherryRooms.GetUsersTotal(rooms[0]) != "1" {
   176  		t.Fail()
   177  	}
   178  	cherryRooms.RemoveUser(rooms[0], "dunha")
   179  	if cherryRooms.GetUsersTotal(rooms[0]) != "0" {
   180  		t.Fail()
   181  	}
   182  	if len(cherryRooms.GetSessionID(rooms[0], "dunha")) != 0 {
   183  		t.Fail()
   184  	}
   185  	message := cherryRooms.GetNextMessage(rooms[0])
   186  	if len(message.From) != 0 ||
   187  		len(message.To) != 0 ||
   188  		len(message.Action) != 0 ||
   189  		len(message.Image) != 0 ||
   190  		len(message.Say) != 0 ||
   191  		len(message.Priv) != 0 {
   192  		t.Fail()
   193  	}
   194  	cherryRooms.EnqueueMessage(rooms[0], "(null)", "(anyone)", "a01", "i01", "boo!", "1")
   195  	message = cherryRooms.GetNextMessage(rooms[0])
   196  	if message.From != "(null)" ||
   197  		message.To != "(anyone)" ||
   198  		message.Action != "a01" ||
   199  		message.Image != "i01" ||
   200  		message.Say != "boo!" ||
   201  		message.Priv != "1" {
   202  		t.Fail()
   203  	}
   204  	for i := 0; i < 2; i++ {
   205  		cherryRooms.DequeueMessage(rooms[0])
   206  		message = cherryRooms.GetNextMessage(rooms[0])
   207  		if len(message.From) != 0 ||
   208  			len(message.To) != 0 ||
   209  			len(message.Action) != 0 ||
   210  			len(message.Image) != 0 ||
   211  			len(message.Say) != 0 ||
   212  			len(message.Priv) != 0 {
   213  			t.Fail()
   214  		}
   215  	}
   216  }