示例#1
0
func (s *XLSuite) TestTemplate(c *C) {

	ctxSs := []string{
		"x\tabc",
		"X\tdef",
	}
	ctxS := strings.Join(ctxSs, "\n") // no terminating newline
	ctx, err := gc.ParseContext(ctxS)
	c.Assert(err, IsNil)
	c.Assert(ctx, NotNil)

	// actually testing context
	c.Assert(ctx.Size(), Equals, len(ctxSs))
	xVal, err := ctx.Lookup("x")
	c.Assert(err, IsNil)
	c.Assert(xVal, Equals, "abc")
	bigXVal, err := ctx.Lookup("X")
	c.Assert(err, IsNil)
	c.Assert(bigXVal, Equals, "def")

	inputSS := []string{
		"this ${x} is ${X} on Sundays",
	}
	expectedSS := []string{
		"this abc is def on Sundays",
	}
	input := strings.Join(inputSS, "\n")
	expected := strings.Join(expectedSS, "\n")

	var rd1 io.Reader = strings.NewReader(input)

	byteBuffer, err := gu.NewByteBuffer(1024)
	c.Assert(err, IsNil)
	c.Assert(byteBuffer, NotNil)
	c.Assert(byteBuffer.Cap(), Equals, 1024)
	c.Assert(byteBuffer.Len(), Equals, 0)

	t, err := NewTemplate(rd1, byteBuffer, ctx)
	c.Assert(err, IsNil)
	c.Assert(t, NotNil)

	err = t.Apply()
	c.Assert(err, Equals, io.EOF)

	output := byteBuffer.String()
	c.Assert(output, Equals, expected)
}
示例#2
0
文件: xgoT.go 项目: jddixon/xgo_go
func main() {
	var (
		context *gc.Context
		ctxData []byte
		err     error
	)

	flag.Usage = Usage
	flag.Parse()
	fileNames := flag.Args()

	// FIXUPS ///////////////////////////////////////////////////////

	// SANITY CHECKS ////////////////////////////////////////////////
	if len(fileNames) == 0 {
		err = NothingToDo
	} else if _, err = os.Stat(*tDir); os.IsNotExist(err) {
		err = SrcDirDoesNotExist
	} else {
		ctxData, err = ioutil.ReadFile(*ctxFile)
		if err == nil {
			context, err = gc.ParseContext(string(ctxData))
		}
	}

	// DISPLAY STUFF ////////////////////////////////////////////////
	if *verbose || *justShow {
		fmt.Printf("bDir         = %v\n", *bDir)
		fmt.Printf("ctxFile      = %v\n", *ctxFile)
		fmt.Printf("inputExt     = %v\n", *inputExt)
		fmt.Printf("justShow     = %v\n", *justShow)
		fmt.Printf("outputExt    = %s\n", *outputExt)
		fmt.Printf("prefix       = %s\n", *prefix)
		fmt.Printf("tDir         = %s\n", *tDir)
		fmt.Printf("testing      = %v\n", *testing)
		fmt.Printf("verbose      = %v\n", *verbose)
		if len(fileNames) > 0 {
			fmt.Print("files: ")
			for i := 0; i < len(fileNames); i++ {
				fmt.Printf("%s ", fileNames[i])
			}
			fmt.Println()
		}
	}

	if err != nil {
		fmt.Printf("\nerror = %s\n", err.Error())
	}
	if err != nil || *justShow {
		return
	}

	// SET UP OPTIONS ///////////////////////////////////////////////
	options := new(gt.Options)
	options.BDir = *bDir
	options.Context = context
	options.FileNames = fileNames
	options.InputExt = *inputExt
	options.JustShow = *justShow
	options.OutputExt = *outputExt
	options.Prefix = *prefix
	options.TDir = *tDir
	options.Testing = *testing
	options.Verbose = *verbose

	// DO USEFUL THINGS /////////////////////////////////////////////
	err = gt.Process(options)
	if err != nil {
		fmt.Printf("\nerror processing input files %s\n", err.Error())
	}
	return
}