github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/go_package_test.go (about)

     1  /*
     2     Golang To Github Markdown Utility: gotomd
     3     Copyright (C) 2023, 2024 Leslie Dancsecs
     4  
     5     This program is free software: you can redistribute it and/or modify
     6     it under the terms of the GNU General Public License as published by
     7     the Free Software Foundation, either version 3 of the License, or
     8     (at your option) any later version.
     9  
    10     This program is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13     GNU General Public License for more details.
    14  
    15     You should have received a copy of the GNU General Public License
    16     along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17  */
    18  
    19  package main
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/dancsecs/sztest"
    25  )
    26  
    27  type docInfoTest struct {
    28  	action  string
    29  	header  []string
    30  	body    []string
    31  	doc     []string
    32  	oneLine string
    33  }
    34  
    35  func Test_GoPackage_GetInfo_InvalidDirectory(t *testing.T) {
    36  	chk := sztest.CaptureNothing(t)
    37  	defer chk.Release()
    38  
    39  	_, err := getInfo("INVALID_DIRECTORY", "TimesTwo")
    40  	chk.Err(err, "open INVALID_DIRECTORY: no such file or directory")
    41  }
    42  
    43  func Test_GoPackage_GetInfo_InvalidObject(t *testing.T) {
    44  	chk := sztest.CaptureNothing(t)
    45  	defer chk.Release()
    46  
    47  	_, err := getInfo("./example1", "DOES_NOT_EXIST")
    48  	chk.Err(err, ErrUnknownObject.Error()+": DOES_NOT_EXIST")
    49  }
    50  
    51  //nolint:funlen,lll // Ok.
    52  func Test_GoPackage_DocInfo_RunTests(t *testing.T) {
    53  	chk := sztest.CaptureNothing(t)
    54  	defer chk.Release()
    55  
    56  	docInfoTests := []docInfoTest{
    57  		//  ----------------------------------------------------------------------
    58  		{
    59  			action: "TimesTwo",
    60  			header: []string{
    61  				"func TimesTwo(i int) int",
    62  			},
    63  			body: []string{
    64  				"func TimesTwo(i int) int {",
    65  				"    return i + i",
    66  				"}",
    67  			},
    68  			doc: []string{
    69  				"TimesTwo returns the value times two.",
    70  			},
    71  			oneLine: "func TimesTwo(i int) int",
    72  		},
    73  		//  ----------------------------------------------------------------------
    74  		{
    75  			action: "TimesThree",
    76  			header: []string{
    77  				"func TimesThree(i int) int",
    78  			},
    79  			body: []string{
    80  				"func TimesThree(i int) int {",
    81  				"    return i + i + i",
    82  				"}",
    83  			},
    84  			doc: []string{
    85  				"TimesThree returns the value times three.",
    86  			},
    87  			oneLine: "func TimesThree(i int) int",
    88  		},
    89  		//  ----------------------------------------------------------------------
    90  		{
    91  			action: "ConstDeclSingleCmtSingle",
    92  			header: nil,
    93  			body: []string{
    94  				"const ConstDeclSingleCmtSingle = " +
    95  					"\"single-line declaration and comment\"",
    96  			},
    97  			doc: []string{
    98  				"ConstDeclSingleCmtSingle has a single-line comment.",
    99  			},
   100  			oneLine: "" +
   101  				"const ConstDeclSingleCmtSingle = " +
   102  				"\"single-line declaration and comment\"",
   103  		},
   104  		//  ----------------------------------------------------------------------
   105  		{
   106  			action: "ConstDeclMultiCmtSingle",
   107  			header: nil,
   108  			body: []string{
   109  				"const ConstDeclMultiCmtSingle = `multiline constant",
   110  				"definition",
   111  				"`",
   112  			},
   113  			doc: []string{
   114  				"ConstDeclMultiCmtSingle has a single-line comment with a multiline decl.",
   115  			},
   116  			oneLine: "" +
   117  				"const ConstDeclMultiCmtSingle = `multiline constant ...",
   118  		},
   119  		//  ----------------------------------------------------------------------
   120  		{
   121  			action: "ConstDeclConstrCmtSingle",
   122  			header: nil,
   123  			body: []string{
   124  				"const ConstDeclConstrCmtSingle = `multiline constant` + \"\n\" +",
   125  				"    ConstDeclMultiCmtSingle + \" including other constants: \n\" +",
   126  				"    ConstDeclSingleCmtSingle + \"\n\" + `",
   127  				"=========end of constant=============",
   128  				"`",
   129  			},
   130  			doc: []string{
   131  				"ConstDeclConstrCmtSingle has a single-line comment with a multiline decl.",
   132  			},
   133  			oneLine: "" +
   134  				"const ConstDeclConstrCmtSingle = `multiline constant` + \"\n\" + ...",
   135  		},
   136  		//  ----------------------------------------------------------------------
   137  		{
   138  			action: "ConstDeclConstrCmtMulti",
   139  			header: nil,
   140  			body: []string{
   141  				"const ConstDeclConstrCmtMulti = `multiline constant` + \"\n\" +",
   142  				"    ConstDeclMultiCmtSingle + \" including other constants: \n\" +",
   143  				"    ConstDeclSingleCmtSingle + \"\n\" + `",
   144  				"=========end of constant=============",
   145  				"`",
   146  			},
   147  			doc: []string{
   148  				"ConstDeclConstrCmtMulti has a multiline comment with",
   149  				"a multiline decl.",
   150  			},
   151  			oneLine: "const ConstDeclConstrCmtMulti =" +
   152  				" `multiline constant` + \"\n\" + ...",
   153  		},
   154  		//  ----------------------------------------------------------------------
   155  		{
   156  			action: "StructureType.GetF1",
   157  			header: []string{
   158  				"func (s *StructureType) GetF1(",
   159  				"    a, b, c int,",
   160  				") string",
   161  			},
   162  			body: []string{
   163  				"func (s *StructureType) GetF1(",
   164  				"    a, b, c int,",
   165  				") string {",
   166  				"    const base10 = 10",
   167  				"",
   168  				"    t := a + c + b",
   169  				"",
   170  				"    return s.F1 + strconv.FormatInt(int64(t), base10)",
   171  				"}",
   172  			},
   173  			doc: []string{
   174  				"GetF1 is a method to a structure.",
   175  			},
   176  			oneLine: "func (s *StructureType) GetF1(a, b, c int) string",
   177  		},
   178  	}
   179  
   180  	for _, tst := range docInfoTests {
   181  		dInfo, err := getInfo("./example1", tst.action)
   182  		chk.NoErr(err)
   183  		chk.StrSlice(dInfo.header, tst.header, "HEADER For action: ", tst.action)
   184  		chk.StrSlice(dInfo.body, tst.body, "BODY For action: ", tst.action)
   185  		chk.StrSlice(dInfo.doc, tst.doc, "DOC For action: ", tst.action)
   186  		chk.Str(dInfo.oneLine(), tst.oneLine, "OneLine For action: ", tst.action)
   187  	}
   188  }