...

Source file src/golang.org/x/text/message/pipeline/testdata/ssa/ssa.go

Documentation: golang.org/x/text/message/pipeline/testdata/ssa

     1  package main
     2  
     3  import (
     4  	"golang.org/x/text/language"
     5  	"golang.org/x/text/message"
     6  )
     7  
     8  // In this test, lowercap strings are ones that need to be picked up for
     9  // translation, whereas uppercap strings should not be picked up.
    10  
    11  func main() {
    12  	p := message.NewPrinter(language.English)
    13  
    14  	// TODO: probably should use type instead of string content for argument
    15  	// substitution.
    16  	wrapf(p, "inline %s", "ARG1")
    17  	gwrapf("global printer used %s", "ARG1")
    18  
    19  	w := wrapped{p}
    20  
    21  	// Comment about wrapf.
    22  	w.wrapf("number: %d, string: %s, bool: %v", 2, "STRING ARG", true)
    23  	w.wrapf("empty string")
    24  	w.wrap("Lovely weather today!")
    25  
    26  	more(&w)
    27  }
    28  
    29  var printer = message.NewPrinter(language.English)
    30  
    31  func more(w wrapper) {
    32  	w.wrap("number one")
    33  	w.wrapf("speed of light: %s", "C")
    34  }
    35  
    36  func gwrapf(format string, args ...interface{}) {
    37  	v := format
    38  	a := args
    39  	printer.Printf(v, a...)
    40  }
    41  
    42  func wrapf(p *message.Printer, format string, args ...interface{}) {
    43  	v := format
    44  	a := args
    45  	p.Printf(v, a...)
    46  }
    47  
    48  func wrap(p *message.Printer, format string) {
    49  	v := format
    50  	b := "0"
    51  	a := []interface{}{3, b}
    52  	s := a[:]
    53  	p.Printf(v, s...)
    54  }
    55  
    56  type wrapper interface {
    57  	wrapf(format string, args ...interface{})
    58  	wrap(msg string)
    59  }
    60  
    61  type wrapped struct {
    62  	p *message.Printer
    63  }
    64  
    65  // TODO: calls over interfaces do not get picked up. It looks like this is
    66  // because w is not a pointer receiver, while the other method is. Mixing of
    67  // receiver types does not seem to be allowed by callgraph/cha.
    68  func (w wrapped) wrapf(format string, args ...interface{}) {
    69  	w.p.Printf(format, args...)
    70  }
    71  
    72  func (w *wrapped) wrap(msg string) {
    73  	w.p.Printf(msg)
    74  }
    75  
    76  func fint(p *message.Printer, x int) {
    77  	v := "number: %d"
    78  	const c = "DAFDA"
    79  	p.Printf(v, c)
    80  }
    81  
    82  const format = "constant local" + " %s"
    83  
    84  // NOTE: pass is not called. Ensure it is picked up anyway.
    85  func pass(p *message.Printer, args ...interface{}) {
    86  	// TODO: find an example caller to find substituted types and argument
    87  	// examples.
    88  	p.Sprintf(format, args...)
    89  }
    90  
    91  func lookup(p *message.Printer, x int) {
    92  	// TODO: pick up all elements from slice foo.
    93  	p.Printf(foo[x])
    94  }
    95  
    96  var foo = []string{
    97  	"aaaa",
    98  	"bbbb",
    99  }
   100  
   101  func field(p *message.Printer, x int) {
   102  	// TODO: pick up strings in field BAR from all composite literals of
   103  	// typeof(strct.Foo.Bar).
   104  	p.Printf(strct.Foo.Bar, x)
   105  }
   106  
   107  type fooStruct struct {
   108  	Foo barStruct
   109  }
   110  
   111  type barStruct struct {
   112  	other int
   113  	Bar   string
   114  }
   115  
   116  var strct = fooStruct{
   117  	Foo: barStruct{0, "foo %d"},
   118  }
   119  
   120  func call(p *message.Printer, x int) {
   121  	// TODO: pick up constant return values.
   122  	p.Printf(fn())
   123  }
   124  
   125  func fn() string {
   126  	return "const str"
   127  }
   128  
   129  // Both strings get picked up.
   130  func ifConst(p *message.Printer, cond bool, arg1 string) {
   131  	a := "foo %s %s"
   132  	if cond {
   133  		a = "bar %s %s"
   134  	}
   135  	b := "FOO"
   136  	if cond {
   137  		b = "BAR"
   138  	}
   139  	wrapf(p, a, arg1, b)
   140  }
   141  
   142  // Pick up all non-empty strings in this function.
   143  func ifConst2(x int) {
   144  	a := ""
   145  	switch x {
   146  	case 0:
   147  		a = "foo"
   148  	case 1:
   149  		a = "bar"
   150  	case 2:
   151  		a = "baz"
   152  	}
   153  	gwrapf(a)
   154  }
   155  
   156  // TODO: pick up strings passed to the second argument in calls to freeVar.
   157  func freeVar(p *message.Printer, str string) {
   158  	fn := func(p *message.Printer) {
   159  		p.Printf(str)
   160  	}
   161  	fn(p)
   162  }
   163  
   164  func freeConst(p *message.Printer) {
   165  	// str is a message
   166  	const str = "const str"
   167  	fn := func(p *message.Printer) {
   168  		p.Printf(str)
   169  	}
   170  	fn(p)
   171  }
   172  
   173  func global(p *message.Printer) {
   174  	// city describes the expected next meeting place
   175  	city := "Amsterdam"
   176  	// See a person around.
   177  	p.Printf(globalStr, city)
   178  }
   179  
   180  // globalStr is a global variable with a string constant assigned to it.
   181  var globalStr = "See you around in %s!"
   182  
   183  func global2(p *message.Printer) {
   184  	const food = "Pastrami"
   185  	wrapf(p, constFood,
   186  		food, // the food to be consumed by the subject
   187  	)
   188  }
   189  
   190  // Comment applying to all constants in a block are ignored.
   191  var (
   192  	// Ho ho ho
   193  	notAMessage, constFood, msgHello = "NOPE!", consume, hello
   194  )
   195  
   196  // A block comment.
   197  var (
   198  	// This comment takes precedence.
   199  	hello = "Hello, %d and %s!"
   200  
   201  	consume = "Please eat your %s!"
   202  )
   203  

View as plain text