github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/identifiers/parser_test.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package identifiers
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestParseBlockNums(t *testing.T) {
    12  	out, err := Parse("10-1000:25")
    13  
    14  	if err != nil {
    15  		t.Error(err)
    16  	}
    17  
    18  	if out.Points[0].Number != 10 {
    19  		t.Error("Mismatched start block:", out.Points[0].Number)
    20  	}
    21  
    22  	if out.Points[1].Number != 1000 {
    23  		t.Error("Mismatched end block:", out.Points[1].Number)
    24  	}
    25  
    26  	if out.Modifier.Step != 25 {
    27  		t.Error("Mismatched step:", out.Modifier.Step)
    28  	}
    29  }
    30  
    31  func TestParseBlockNumsNoEnd(t *testing.T) {
    32  	out, err := Parse("10:25")
    33  
    34  	if err != nil {
    35  		t.Error(err)
    36  	}
    37  
    38  	if out.Points[0].Number != 10 {
    39  		t.Error("Mismatched start block:", out.Points[0].Number)
    40  	}
    41  
    42  	if len(out.Points) > 1 {
    43  		t.Error("End present in parsed output")
    44  	}
    45  
    46  	if out.Modifier.Step != 25 {
    47  		t.Error("Mismatched step:", out.Modifier.Step)
    48  	}
    49  }
    50  
    51  func TestParseBlockNumsNoStep(t *testing.T) {
    52  	out, err := Parse("10-1000")
    53  
    54  	if err != nil {
    55  		t.Error(err)
    56  	}
    57  
    58  	if out.Points[0].Number != 10 {
    59  		t.Error("Mismatched start block:", out.Points[0].Number)
    60  	}
    61  
    62  	if out.Points[1].Number != 1000 {
    63  		t.Error("Mismatched end block:", out.Points[1].Number)
    64  	}
    65  
    66  	if out.Modifier != nil {
    67  		t.Error("Modifier present in parsed output")
    68  	}
    69  }
    70  
    71  func TestParseBlockNumsNoEndNoStep(t *testing.T) {
    72  	out, err := Parse("10")
    73  
    74  	if err != nil {
    75  		t.Error(err)
    76  	}
    77  
    78  	if out.Points[0].Number != 10 {
    79  		t.Error("Mismatched start block:", out.Points[0].Number)
    80  	}
    81  
    82  	if len(out.Points) > 1 {
    83  		t.Error("End present in parsed output")
    84  	}
    85  
    86  	if out.Modifier != nil {
    87  		t.Error("Modifier present in parsed output")
    88  	}
    89  }
    90  
    91  func TestParseBlockNumsPeriod(t *testing.T) {
    92  	out, err := Parse("10-1000:weekly")
    93  
    94  	if err != nil {
    95  		t.Error(err)
    96  	}
    97  
    98  	if out.Points[0].Number != 10 {
    99  		t.Error("Mismatched start block:", out.Points[0].Number)
   100  	}
   101  
   102  	if out.Points[1].Number != 1000 {
   103  		t.Error("Mismatched end block:", out.Points[1].Number)
   104  	}
   105  
   106  	if out.Modifier.Period != "weekly" {
   107  		t.Error("Mismatched period:", out.Modifier.Period)
   108  	}
   109  }
   110  
   111  func TestParseBlockNumsPeriodNoEnd(t *testing.T) {
   112  	out, err := Parse("10:weekly")
   113  
   114  	if err != nil {
   115  		t.Error(err)
   116  	}
   117  
   118  	if out.Points[0].Number != 10 {
   119  		t.Error("Mismatched start block:", out.Points[0].Number)
   120  	}
   121  
   122  	if len(out.Points) > 1 {
   123  		t.Error("End present in parsed output")
   124  	}
   125  
   126  	if out.Modifier.Period != "weekly" {
   127  		t.Error("Mismatched period:", out.Modifier.Period)
   128  	}
   129  }
   130  
   131  func TestParseBlockNumsHexStart(t *testing.T) {
   132  	out, err := Parse("0xfff-1000:25")
   133  
   134  	if err != nil {
   135  		t.Error(err)
   136  	}
   137  
   138  	if out.Points[0].Number != 4095 {
   139  		t.Error("Mismatched start block:", out.Points[0].Number)
   140  	}
   141  
   142  	if out.Points[1].Number != 1000 {
   143  		t.Error("Mismatched end block:", out.Points[1].Number)
   144  	}
   145  
   146  	if out.Modifier.Step != 25 {
   147  		t.Error("Mismatched step:", out.Modifier.Step)
   148  	}
   149  }
   150  
   151  func TestParseSpecialStart(t *testing.T) {
   152  	out, err := Parse("london-1000:10")
   153  
   154  	if err != nil {
   155  		t.Error(err)
   156  	}
   157  
   158  	if out.Points[0].Special != "london" {
   159  		t.Error("Mismatched start special:", out.Points[0].Special)
   160  	}
   161  
   162  	if out.Points[1].Number != 1000 {
   163  		t.Error("Mismatched end block:", out.Points[1].Number)
   164  	}
   165  
   166  	if out.Modifier.Step != 10 {
   167  		t.Error("Mismatched step:", out.Modifier.Step)
   168  	}
   169  }
   170  
   171  func TestParseSpecialEnd(t *testing.T) {
   172  	out, err := Parse("0-devcon1:10")
   173  
   174  	if err != nil {
   175  		t.Error(err)
   176  	}
   177  
   178  	if out.Points[0].Number != 0 {
   179  		t.Error("Mismatched start block:", out.Points[0].Number)
   180  	}
   181  
   182  	if out.Points[1].Special != "devcon1" {
   183  		t.Error("Mismatched end special:", out.Points[1].Special)
   184  	}
   185  
   186  	if out.Modifier.Step != 10 {
   187  		t.Error("Mismatched step:", out.Modifier.Step)
   188  	}
   189  }
   190  
   191  func TestParseSpecialNoEndNoModifier(t *testing.T) {
   192  	out, err := Parse("london")
   193  
   194  	if err != nil {
   195  		t.Error(err)
   196  	}
   197  
   198  	if out.Points[0].Special != "london" {
   199  		t.Error("Mismatched start special:", out.Points[0].Special)
   200  	}
   201  
   202  	if len(out.Points) > 1 {
   203  		t.Error("End present in parsed output")
   204  	}
   205  
   206  	if out.Modifier != nil {
   207  		t.Error("Modifier present in parsed output")
   208  	}
   209  }
   210  
   211  func TestParseSpecialNoEnd(t *testing.T) {
   212  	out, err := Parse("devcon1:10")
   213  
   214  	if err != nil {
   215  		t.Error(err)
   216  	}
   217  
   218  	if out.Points[0].Special != "devcon1" {
   219  		t.Error("Mismatched start block:", out.Points[0].Special)
   220  	}
   221  
   222  	if len(out.Points) > 1 {
   223  		t.Error("End present in parsed output")
   224  	}
   225  
   226  	if out.Modifier.Step != 10 {
   227  		t.Error("Mismatched step:", out.Modifier.Step)
   228  	}
   229  }
   230  
   231  func TestParseDate(t *testing.T) {
   232  	out, err := Parse("10-2021-10-03T10:20:30:25")
   233  
   234  	if err != nil {
   235  		t.Error(err)
   236  	}
   237  
   238  	if out.Points[0].Number != 10 {
   239  		t.Error("Mismatched start block:", out.Points[0].Number)
   240  	}
   241  
   242  	if out.Points[1].Date != "2021-10-03T10:20:30" {
   243  		t.Error("Mismatched end date:", out.Points[1].Date)
   244  	}
   245  
   246  	if out.Modifier.Step != 25 {
   247  		t.Error("Mismatched step:", out.Modifier.Step)
   248  	}
   249  }
   250  
   251  func TestParseDateUTC(t *testing.T) {
   252  	out, err := Parse("10-2021-10-03T10:20:30UTC:25")
   253  
   254  	if err != nil {
   255  		t.Error(err)
   256  	}
   257  
   258  	if out.Points[0].Number != 10 {
   259  		t.Error("Mismatched start block:", out.Points[0].Number)
   260  	}
   261  
   262  	if out.Points[1].Date != "2021-10-03T10:20:30UTC" {
   263  		t.Error("Mismatched end date:", out.Points[1].Date)
   264  	}
   265  
   266  	if out.Modifier.Step != 25 {
   267  		t.Error("Mismatched step:", out.Modifier.Step)
   268  	}
   269  }
   270  
   271  // func TestParseDateYearMonth(t *testing.T) {
   272  // 	out, err := Parse("10-2021-10:25")
   273  
   274  // 	if err != nil {
   275  // 		t.Error(err)
   276  // 	}
   277  
   278  // 	if out.Points[0].Block != 10 {
   279  // 		t.Error("Mismatched start block:", out.Points[0].Block)
   280  // 	}
   281  
   282  // 	if out.Points[1].Date != "2021-10" {
   283  // 		t.Error("Mismatched end date:", out.Points[1].Date)
   284  // 	}
   285  
   286  // 	if out.Modifier.Step != 25 {
   287  // 		t.Error("Mismatched step:", out.Modifier.Step)
   288  // 	}
   289  // }
   290  
   291  func TestParseDateShort(t *testing.T) {
   292  	out, err := Parse("10-2021-10-03:25")
   293  
   294  	if err != nil {
   295  		t.Error(err)
   296  	}
   297  
   298  	if out.Points[0].Number != 10 {
   299  		t.Error("Mismatched start block:", out.Points[0].Number)
   300  	}
   301  
   302  	if out.Points[1].Date != "2021-10-03" {
   303  		t.Error("Mismatched end date:", out.Points[1].Date)
   304  	}
   305  
   306  	if out.Modifier.Step != 25 {
   307  		t.Error("Mismatched step:", out.Modifier.Step)
   308  	}
   309  }
   310  
   311  func TestParseDateOnlyHour(t *testing.T) {
   312  	out, err := Parse("10-2021-10-03T05")
   313  
   314  	if err != nil {
   315  		t.Error(err)
   316  	}
   317  
   318  	if out.Points[0].Number != 10 {
   319  		t.Error("Mismatched start block:", out.Points[0].Number)
   320  	}
   321  
   322  	if out.Points[1].Date != "2021-10-03T05" {
   323  		t.Error("Mismatched end date:", out.Points[1].Date)
   324  	}
   325  
   326  	if out.Modifier != nil {
   327  		t.Error("Modifier present in parsed output")
   328  	}
   329  }
   330  
   331  func TestParseDateOnlyHourAndMinutes(t *testing.T) {
   332  	out, err := Parse("10-2021-10-03T05:59")
   333  
   334  	if err != nil {
   335  		t.Error(err)
   336  	}
   337  
   338  	if out.Points[0].Number != 10 {
   339  		t.Error("Mismatched start block:", out.Points[0].Number)
   340  	}
   341  
   342  	if out.Points[1].Date != "2021-10-03T05:59" {
   343  		t.Error("Mismatched end date:", out.Points[1].Date)
   344  	}
   345  
   346  	if out.Modifier != nil {
   347  		t.Error("Modifier present in parsed output")
   348  	}
   349  }
   350  
   351  func TestParseDateNoModifier(t *testing.T) {
   352  	out, err := Parse("10-2021-10-03T05:59:29")
   353  
   354  	if err != nil {
   355  		t.Error(err)
   356  	}
   357  
   358  	if out.Points[0].Number != 10 {
   359  		t.Error("Mismatched start block:", out.Points[0].Number)
   360  	}
   361  
   362  	if out.Points[1].Date != "2021-10-03T05:59:29" {
   363  		t.Error("Mismatched end date:", out.Points[1].Date)
   364  	}
   365  
   366  	if out.Modifier != nil {
   367  		t.Error("Modifier present in parsed output")
   368  	}
   369  }
   370  
   371  func TestParseDateNoEndNoModifier(t *testing.T) {
   372  	out, err := Parse("2017-10-03T10:20:30")
   373  
   374  	if err != nil {
   375  		t.Error(err)
   376  	}
   377  
   378  	if out.Points[0].Date != "2017-10-03T10:20:30" {
   379  		t.Error("Mismatched start block:", out.Points[0].Date)
   380  	}
   381  
   382  	if len(out.Points) > 1 {
   383  		t.Error("End present in parsed output")
   384  	}
   385  
   386  	if out.Modifier != nil {
   387  		t.Error("Modifier present in parsed output")
   388  	}
   389  }
   390  
   391  func TestParseShortDateNoEndNoModifier(t *testing.T) {
   392  	out, err := Parse("2017-10-03")
   393  
   394  	if err != nil {
   395  		t.Error(err)
   396  	}
   397  
   398  	if out.Points[0].Date != "2017-10-03" {
   399  		t.Error("Mismatched start block:", out.Points[0].Date)
   400  	}
   401  
   402  	if len(out.Points) > 1 {
   403  		t.Error("End present in parsed output")
   404  	}
   405  
   406  	if out.Modifier != nil {
   407  		t.Error("Modifier present in parsed output")
   408  	}
   409  }
   410  
   411  func TestParseDateStart(t *testing.T) {
   412  	out, err := Parse("2017-10-03T10:20:30-1000:25")
   413  
   414  	if err != nil {
   415  		t.Error(err)
   416  	}
   417  
   418  	if out.Points[0].Date != "2017-10-03T10:20:30" {
   419  		t.Error("Mismatched start block:", out.Points[0].Date)
   420  	}
   421  
   422  	if out.Points[1].Number != 1000 {
   423  		t.Error("Mismatched end date:", out.Points[1].Number)
   424  	}
   425  
   426  	if out.Modifier.Step != 25 {
   427  		t.Error("Mismatched step:", out.Modifier.Step)
   428  	}
   429  }
   430  
   431  func TestParseDateStartShort(t *testing.T) {
   432  	out, err := Parse("2017-10-03-1000:25")
   433  
   434  	if err != nil {
   435  		t.Error(err)
   436  	}
   437  
   438  	if out.Points[0].Date != "2017-10-03" {
   439  		t.Error("Mismatched start block:", out.Points[0].Date)
   440  	}
   441  
   442  	if out.Points[1].Number != 1000 {
   443  		t.Error("Mismatched end date:", out.Points[1].Number)
   444  	}
   445  
   446  	if out.Modifier.Step != 25 {
   447  		t.Error("Mismatched step:", out.Modifier.Step)
   448  	}
   449  }
   450  
   451  func TestParseInvalidInput(t *testing.T) {
   452  	_, err := Parse("-100")
   453  
   454  	if err == nil {
   455  		t.Error("Passes for -100")
   456  	}
   457  }
   458  
   459  func TestParseInvalidPeriod(t *testing.T) {
   460  	_, err := Parse("10-100:biweekly")
   461  
   462  	if err == nil {
   463  		t.Error(err)
   464  		t.Error("Passes for invalid period")
   465  	}
   466  }