github.com/coyove/common@v0.0.0-20240403014525-f70e643f9de8/config/conf_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestConfParsing(t *testing.T) {
     8  	t.Log("Test conf file parsing")
     9  
    10  	var cf *conf_t
    11  	var err error
    12  	var text = `a=b#comment
    13  	b=1
    14  	c
    15  	d=#comment2
    16  	[section]
    17  	e f = 12
    18  	g=on
    19  	a='on'`
    20  
    21  	throw := func() { t.Error("Error parsing:", text) }
    22  
    23  	cf, err = ParseConf(text)
    24  	if err != nil {
    25  		throw()
    26  	}
    27  
    28  	if cf.GetString("default", "a", "") != "b" {
    29  		throw()
    30  	}
    31  
    32  	if cf.GetString("default", "c", "non-empty") != "" {
    33  		throw()
    34  	}
    35  
    36  	if cf.GetString("default", "d", "non-empty") != "" {
    37  		throw()
    38  	}
    39  
    40  	if cf.GetInt("section", "ef", 0) != 12 {
    41  		throw()
    42  	}
    43  
    44  	if cf.GetBool("section", "g", false) != true {
    45  		throw()
    46  	}
    47  
    48  	if cf.GetString("section", "a", "") != "on" {
    49  		throw()
    50  	}
    51  
    52  	text = `a="ab\"sd"
    53  	b='single-quote\n\tnew-line'
    54  	c="#=@#!$#*("
    55  
    56  	d=1
    57  	d=2
    58  	d=3
    59  	d=4
    60  	d=5`
    61  
    62  	cf, err = ParseConf(text)
    63  	if err != nil {
    64  		throw()
    65  	}
    66  
    67  	if cf.GetString("default", "a", "") != `ab"sd` {
    68  		throw()
    69  	}
    70  
    71  	if cf.GetString("default", "b", "") != "single-quote\n\tnew-line" {
    72  		throw()
    73  	}
    74  
    75  	if cf.GetString("default", "c", "") != "#=@#!$#*(" {
    76  		throw()
    77  	}
    78  
    79  	for i, num := range cf.GetArray("default", "d") {
    80  		if n, ok := num.(float64); ok {
    81  			if int(n) == i+1 {
    82  				continue
    83  			}
    84  		}
    85  
    86  		throw()
    87  	}
    88  
    89  	text = `[sect#ion
    90  	a=b`
    91  
    92  	cf, err = ParseConf(text)
    93  	if err != nil && err.(*ConfError).text == "[" {
    94  		// ok
    95  	} else {
    96  		throw()
    97  	}
    98  
    99  	text = `a= ='#v'`
   100  
   101  	cf, err = ParseConf(text)
   102  	if err != nil && err.(*ConfError).text == "=" {
   103  		// ok
   104  	} else {
   105  		throw()
   106  	}
   107  
   108  	text = `a= 'text\'`
   109  
   110  	cf, err = ParseConf(text)
   111  	if err != nil && err.(*ConfError).text == "'" {
   112  		// ok
   113  	} else {
   114  		throw()
   115  	}
   116  
   117  	text = `a= "incomplete #comment`
   118  
   119  	cf, err = ParseConf(text)
   120  	if err != nil && err.(*ConfError).text == "\"" {
   121  		// ok
   122  	} else {
   123  		throw()
   124  	}
   125  }