How to use Text method of internal Package

Best Ginkgo code snippet using internal.Text

deps_test.go

Source:deps_test.go Github

copy

Full Screen

1// Copyright 2012 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4// This file exercises the import parser but also checks that5// some low-level packages do not have new dependencies added.6package build7import (8 "bytes"9 "fmt"10 "go/token"11 "internal/testenv"12 "io/fs"13 "os"14 "path/filepath"15 "runtime"16 "sort"17 "strings"18 "testing"19)20// depsRules defines the expected dependencies between packages in21// the Go source tree. It is a statement of policy.22//23// DO NOT CHANGE THIS DATA TO FIX BUILDS.24// Existing packages should not have their constraints relaxed25// without prior discussion.26// Negative assertions should almost never be removed.27//28// The general syntax of a rule is:29//30// a, b < c, d;31//32// which means c and d come after a and b in the partial order33// (that is, c and d can import a and b),34// but doesn't provide a relative order between a vs b or c vs d.35//36// The rules can chain together, as in:37//38// e < f, g < h;39//40// which is equivalent to41//42// e < f, g;43// f, g < h;44//45// Except for the special bottom element "NONE", each name46// must appear exactly once on the right-hand side of a rule.47// That rule serves as the definition of the allowed dependencies48// for that name. The definition must appear before any uses49// of the name on the left-hand side of a rule. (That is, the50// rules themselves must be ordered according to the partial51// order, for easier reading by people.)52//53// Negative assertions double-check the partial order:54//55// i !< j56//57// means that it must NOT be the case that i < j.58// Negative assertions may appear anywhere in the rules,59// even before i and j have been defined.60//61// Comments begin with #.62//63// All-caps names are pseudo-names for specific points64// in the dependency lattice.65//66var depsRules = `67 # No dependencies allowed for any of these packages.68 NONE69 < container/list, container/ring,70 internal/cfg, internal/cpu, internal/goexperiment,71 internal/goversion, internal/nettrace,72 unicode/utf8, unicode/utf16, unicode,73 unsafe;74 # These packages depend only on unsafe.75 unsafe76 < internal/abi;77 # RUNTIME is the core runtime group of packages, all of them very light-weight.78 internal/abi, internal/cpu, internal/goexperiment, unsafe79 < internal/bytealg80 < internal/itoa81 < internal/unsafeheader82 < runtime/internal/sys83 < runtime/internal/atomic84 < runtime/internal/math85 < runtime86 < sync/atomic87 < internal/race88 < sync89 < internal/reflectlite90 < errors91 < internal/oserror, math/bits92 < RUNTIME;93 RUNTIME94 < sort95 < container/heap;96 RUNTIME97 < io;98 syscall !< io;99 reflect !< sort;100 RUNTIME, unicode/utf8101 < path;102 unicode !< path;103 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.104 RUNTIME, unicode/utf8, unicode/utf16105 < internal/syscall/windows/sysdll, syscall/js106 < syscall107 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry108 < internal/syscall/execenv109 < SYSCALL;110 # TIME is SYSCALL plus the core packages about time, including context.111 SYSCALL112 < time/tzdata113 < time114 < context115 < TIME;116 TIME, io, path, sort117 < io/fs;118 # MATH is RUNTIME plus the basic math packages.119 RUNTIME120 < math121 < MATH;122 unicode !< math;123 MATH124 < math/cmplx;125 MATH126 < math/rand;127 MATH128 < runtime/metrics;129 MATH, unicode/utf8130 < strconv;131 unicode !< strconv;132 # STR is basic string and buffer manipulation.133 RUNTIME, io, unicode/utf8, unicode/utf16, unicode134 < bytes, strings135 < bufio;136 bufio, path, strconv137 < STR;138 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).139 # OS includes string routines, but those must be layered above package os.140 # OS does not include reflection.141 io/fs142 < internal/testlog143 < internal/poll144 < os145 < os/signal;146 io/fs147 < embed;148 unicode, fmt !< os, os/signal;149 os/signal, STR150 < path/filepath151 < io/ioutil, os/exec;152 io/ioutil, os/exec, os/signal153 < OS;154 reflect !< OS;155 OS156 < golang.org/x/sys/cpu;157 # FMT is OS (which includes string routines) plus reflect and fmt.158 # It does not include package log, which should be avoided in core packages.159 strconv, unicode160 < reflect;161 os, reflect162 < internal/fmtsort163 < fmt;164 OS, fmt165 < FMT;166 log !< FMT;167 OS, FMT168 < internal/execabs;169 OS, internal/execabs170 < internal/goroot;171 # Misc packages needing only FMT.172 FMT173 < flag,174 html,175 mime/quotedprintable,176 net/internal/socktest,177 net/url,178 runtime/debug,179 runtime/trace,180 text/scanner,181 text/tabwriter;182 # encodings183 # core ones do not use fmt.184 io, strconv185 < encoding;186 encoding, reflect187 < encoding/binary188 < encoding/base32, encoding/base64;189 fmt !< encoding/base32, encoding/base64;190 FMT, encoding/base32, encoding/base64191 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,192 encoding/json, encoding/pem, encoding/xml, mime;193 # hashes194 io195 < hash196 < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;197 # math/big198 FMT, encoding/binary, math/rand199 < math/big;200 # compression201 FMT, encoding/binary, hash/adler32, hash/crc32202 < compress/bzip2, compress/flate, compress/lzw203 < archive/zip, compress/gzip, compress/zlib;204 # templates205 FMT206 < text/template/parse;207 net/url, text/template/parse208 < text/template209 < internal/lazytemplate;210 encoding/json, html, text/template211 < html/template;212 # regexp213 FMT214 < regexp/syntax215 < regexp216 < internal/lazyregexp;217 # suffix array218 encoding/binary, regexp219 < index/suffixarray;220 # executable parsing221 FMT, encoding/binary, compress/zlib222 < debug/dwarf223 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff224 < DEBUG;225 # go parser and friends.226 FMT227 < go/token228 < go/scanner229 < go/ast230 < go/internal/typeparams231 < go/parser;232 FMT233 < go/build/constraint;234 go/build/constraint, go/parser, text/tabwriter235 < go/printer236 < go/format;237 go/parser, internal/lazyregexp, text/template238 < go/doc;239 math/big, go/token240 < go/constant;241 container/heap, go/constant, go/parser, regexp242 < go/types;243 FMT, internal/goexperiment244 < internal/buildcfg;245 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion246 < go/build;247 DEBUG, go/build, go/types, text/scanner248 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter249 < go/importer;250 # databases251 FMT252 < database/sql/internal253 < database/sql/driver254 < database/sql;255 # images256 FMT, compress/lzw, compress/zlib257 < image/color258 < image, image/color/palette259 < image/internal/imageutil260 < image/draw261 < image/gif, image/jpeg, image/png;262 # cgo, delayed as long as possible.263 # If you add a dependency on CGO, you must add the package264 # to cgoPackages in cmd/dist/test.go as well.265 RUNTIME266 < C267 < runtime/cgo268 < CGO269 < runtime/race, runtime/msan;270 # Bulk of the standard library must not use cgo.271 # The prohibition stops at net and os/user.272 C !< fmt, go/types, CRYPTO-MATH;273 CGO, OS274 < plugin;275 CGO, FMT276 < os/user277 < archive/tar;278 sync279 < internal/singleflight;280 os281 < golang.org/x/net/dns/dnsmessage,282 golang.org/x/net/lif,283 golang.org/x/net/route;284 # net is unavoidable when doing any networking,285 # so large dependencies must be kept out.286 # This is a long-looking list but most of these287 # are small with few dependencies.288 CGO,289 golang.org/x/net/dns/dnsmessage,290 golang.org/x/net/lif,291 golang.org/x/net/route,292 internal/nettrace,293 internal/poll,294 internal/singleflight,295 internal/race,296 os297 < net;298 fmt, unicode !< net;299 math/rand !< net; # net uses runtime instead300 # NET is net plus net-helper packages.301 FMT, net302 < net/textproto;303 mime, net/textproto, net/url304 < NET;305 # logging - most packages should not import; http and up is allowed306 FMT307 < log;308 log !< crypto/tls, database/sql, go/importer, testing;309 FMT, log, net310 < log/syslog;311 NET, log312 < net/mail;313 # CRYPTO is core crypto algorithms - no cgo, fmt, net.314 # Unfortunately, stuck with reflect via encoding/binary.315 encoding/binary, golang.org/x/sys/cpu, hash316 < crypto317 < crypto/subtle318 < crypto/internal/subtle319 < crypto/elliptic/internal/fiat320 < crypto/ed25519/internal/edwards25519/field321 < crypto/ed25519/internal/edwards25519322 < crypto/cipher323 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,324 crypto/sha1, crypto/sha256, crypto/sha512325 < CRYPTO;326 CGO, fmt, net !< CRYPTO;327 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.328 CRYPTO, FMT, math/big329 < crypto/rand330 < crypto/internal/randutil331 < crypto/ed25519332 < encoding/asn1333 < golang.org/x/crypto/cryptobyte/asn1334 < golang.org/x/crypto/cryptobyte335 < golang.org/x/crypto/curve25519336 < crypto/dsa, crypto/elliptic, crypto/rsa337 < crypto/ecdsa338 < CRYPTO-MATH;339 CGO, net !< CRYPTO-MATH;340 # TLS, Prince of Dependencies.341 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem342 < golang.org/x/crypto/internal/subtle343 < golang.org/x/crypto/chacha20344 < golang.org/x/crypto/poly1305345 < golang.org/x/crypto/chacha20poly1305346 < golang.org/x/crypto/hkdf347 < crypto/x509/internal/macos348 < crypto/x509/pkix349 < crypto/x509350 < crypto/tls;351 # crypto-aware packages352 NET, crypto/rand, mime/quotedprintable353 < mime/multipart;354 crypto/tls355 < net/smtp;356 # HTTP, King of Dependencies.357 FMT358 < golang.org/x/net/http2/hpack359 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;360 FMT, NET, container/list, encoding/binary, log361 < golang.org/x/text/transform362 < golang.org/x/text/unicode/norm363 < golang.org/x/text/unicode/bidi364 < golang.org/x/text/secure/bidirule365 < golang.org/x/net/idna366 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;367 NET, crypto/tls368 < net/http/httptrace;369 compress/gzip,370 golang.org/x/net/http/httpguts,371 golang.org/x/net/http/httpproxy,372 golang.org/x/net/http2/hpack,373 net/http/internal,374 net/http/internal/ascii,375 net/http/internal/testcert,376 net/http/httptrace,377 mime/multipart,378 log379 < net/http;380 # HTTP-aware packages381 encoding/json, net/http382 < expvar;383 net/http, net/http/internal/ascii384 < net/http/cookiejar, net/http/httputil;385 net/http, flag386 < net/http/httptest;387 net/http, regexp388 < net/http/cgi389 < net/http/fcgi;390 # Profiling391 FMT, compress/gzip, encoding/binary, text/tabwriter392 < runtime/pprof;393 OS, compress/gzip, regexp394 < internal/profile;395 html, internal/profile, net/http, runtime/pprof, runtime/trace396 < net/http/pprof;397 # RPC398 encoding/gob, encoding/json, go/token, html/template, net/http399 < net/rpc400 < net/rpc/jsonrpc;401 # System Information402 internal/cpu, sync403 < internal/sysinfo;404 # Test-only405 log406 < testing/iotest407 < testing/fstest;408 FMT, flag, math/rand409 < testing/quick;410 FMT, flag, runtime/debug, runtime/trace, internal/sysinfo, math/rand411 < testing;412 internal/testlog, runtime/pprof, regexp413 < testing/internal/testdeps;414 OS, flag, testing, internal/cfg415 < internal/testenv;416 OS, encoding/base64417 < internal/obscuretestdata;418 CGO, OS, fmt419 < os/signal/internal/pty;420 NET, testing, math/rand421 < golang.org/x/net/nettest;422 FMT, container/heap, math/rand423 < internal/trace;424`425// listStdPkgs returns the same list of packages as "go list std".426func listStdPkgs(goroot string) ([]string, error) {427 // Based on cmd/go's matchPackages function.428 var pkgs []string429 src := filepath.Join(goroot, "src") + string(filepath.Separator)430 walkFn := func(path string, d fs.DirEntry, err error) error {431 if err != nil || !d.IsDir() || path == src {432 return nil433 }434 base := filepath.Base(path)435 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {436 return filepath.SkipDir437 }438 name := filepath.ToSlash(path[len(src):])439 if name == "builtin" || name == "cmd" {440 return filepath.SkipDir441 }442 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))443 return nil444 }445 if err := filepath.WalkDir(src, walkFn); err != nil {446 return nil, err447 }448 return pkgs, nil449}450func TestDependencies(t *testing.T) {451 if !testenv.HasSrc() {452 // Tests run in a limited file system and we do not453 // provide access to every source file.454 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)455 }456 ctxt := Default457 all, err := listStdPkgs(ctxt.GOROOT)458 if err != nil {459 t.Fatal(err)460 }461 sort.Strings(all)462 sawImport := map[string]map[string]bool{} // from package => to package => true463 policy := depsPolicy(t)464 for _, pkg := range all {465 imports, err := findImports(pkg)466 if err != nil {467 t.Error(err)468 continue469 }470 if sawImport[pkg] == nil {471 sawImport[pkg] = map[string]bool{}472 }473 ok := policy[pkg]474 var bad []string475 for _, imp := range imports {476 sawImport[pkg][imp] = true477 if !ok[imp] {478 bad = append(bad, imp)479 }480 }481 if bad != nil {482 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)483 }484 }485 // depPath returns the path between the given from and to packages.486 // It returns the empty string if there's no dependency path.487 var depPath func(string, string) string488 depPath = func(from, to string) string {489 if sawImport[from][to] {490 return from + " => " + to491 }492 for pkg := range sawImport[from] {493 if p := depPath(pkg, to); p != "" {494 return from + " => " + p495 }496 }497 return ""498 }499}500var buildIgnore = []byte("\n// +build ignore")501func findImports(pkg string) ([]string, error) {502 vpkg := pkg503 if strings.HasPrefix(pkg, "golang.org") {504 vpkg = "vendor/" + pkg505 }506 dir := filepath.Join(Default.GOROOT, "src", vpkg)507 files, err := os.ReadDir(dir)508 if err != nil {509 return nil, err510 }511 var imports []string512 var haveImport = map[string]bool{}513 fset := token.NewFileSet()514 for _, file := range files {515 name := file.Name()516 if name == "slice_go14.go" || name == "slice_go18.go" {517 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.518 continue519 }520 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {521 continue522 }523 info := fileInfo{524 name: filepath.Join(dir, name),525 fset: fset,526 }527 f, err := os.Open(info.name)528 if err != nil {529 return nil, err530 }531 err = readGoInfo(f, &info)532 f.Close()533 if err != nil {534 return nil, fmt.Errorf("reading %v: %v", name, err)535 }536 if bytes.Contains(info.header, buildIgnore) {537 continue538 }539 for _, imp := range info.imports {540 path := imp.path541 if !haveImport[path] {542 haveImport[path] = true543 imports = append(imports, path)544 }545 }546 }547 sort.Strings(imports)548 return imports, nil549}550// depsPolicy returns a map m such that m[p][d] == true when p can import d.551func depsPolicy(t *testing.T) map[string]map[string]bool {552 allowed := map[string]map[string]bool{"NONE": {}}553 disallowed := [][2][]string{}554 parseDepsRules(t, func(deps []string, op string, users []string) {555 if op == "!<" {556 disallowed = append(disallowed, [2][]string{deps, users})557 return558 }559 for _, u := range users {560 if allowed[u] != nil {561 t.Errorf("multiple deps lists for %s", u)562 }563 allowed[u] = make(map[string]bool)564 for _, d := range deps {565 if allowed[d] == nil {566 t.Errorf("use of %s before its deps list", d)567 }568 allowed[u][d] = true569 }570 }571 })572 // Check for missing deps info.573 for _, deps := range allowed {574 for d := range deps {575 if allowed[d] == nil {576 t.Errorf("missing deps list for %s", d)577 }578 }579 }580 // Complete transitive allowed deps.581 for k := range allowed {582 for i := range allowed {583 for j := range allowed {584 if i != k && k != j && allowed[i][k] && allowed[k][j] {585 if i == j {586 // Can only happen along with a "use of X before deps" error above,587 // but this error is more specific - it makes clear that reordering the588 // rules will not be enough to fix the problem.589 t.Errorf("deps policy cycle: %s < %s < %s", j, k, i)590 }591 allowed[i][j] = true592 }593 }594 }595 }596 // Check negative assertions against completed allowed deps.597 for _, bad := range disallowed {598 deps, users := bad[0], bad[1]599 for _, d := range deps {600 for _, u := range users {601 if allowed[u][d] {602 t.Errorf("deps policy incorrect: assertion failed: %s !< %s", d, u)603 }604 }605 }606 }607 if t.Failed() {608 t.FailNow()609 }610 return allowed611}612// parseDepsRules parses depsRules, calling save(deps, op, users)613// for each deps < users or deps !< users rule614// (op is "<" or "!<").615func parseDepsRules(t *testing.T, save func(deps []string, op string, users []string)) {616 p := &depsParser{t: t, lineno: 1, text: depsRules}617 var prev []string618 var op string619 for {620 list, tok := p.nextList()621 if tok == "" {622 if prev == nil {623 break624 }625 p.syntaxError("unexpected EOF")626 }627 if prev != nil {628 save(prev, op, list)629 }630 prev = list631 if tok == ";" {632 prev = nil633 op = ""634 continue635 }636 if tok != "<" && tok != "!<" {637 p.syntaxError("missing <")638 }639 op = tok640 }641}642// A depsParser parses the depsRules syntax described above.643type depsParser struct {644 t *testing.T645 lineno int646 lastWord string647 text string648}649// syntaxError reports a parsing error.650func (p *depsParser) syntaxError(msg string) {651 p.t.Fatalf("deps:%d: syntax error: %s near %s", p.lineno, msg, p.lastWord)652}653// nextList parses and returns a comma-separated list of names.654func (p *depsParser) nextList() (list []string, token string) {655 for {656 tok := p.nextToken()657 switch tok {658 case "":659 if len(list) == 0 {660 return nil, ""661 }662 fallthrough663 case ",", "<", "!<", ";":664 p.syntaxError("bad list syntax")665 }666 list = append(list, tok)667 tok = p.nextToken()668 if tok != "," {669 return list, tok670 }671 }672}673// nextToken returns the next token in the deps rules,674// one of ";" "," "<" "!<" or a name.675func (p *depsParser) nextToken() string {676 for {677 if p.text == "" {678 return ""679 }680 switch p.text[0] {681 case ';', ',', '<':682 t := p.text[:1]683 p.text = p.text[1:]684 return t685 case '!':686 if len(p.text) < 2 || p.text[1] != '<' {687 p.syntaxError("unexpected token !")688 }689 p.text = p.text[2:]690 return "!<"691 case '#':692 i := strings.Index(p.text, "\n")693 if i < 0 {694 i = len(p.text)695 }696 p.text = p.text[i:]697 continue698 case '\n':699 p.lineno++700 fallthrough701 case ' ', '\t':702 p.text = p.text[1:]703 continue704 default:705 i := strings.IndexAny(p.text, "!;,<#\n \t")706 if i < 0 {707 i = len(p.text)708 }709 t := p.text[:i]710 p.text = p.text[i:]711 p.lastWord = t712 return t713 }714 }715}716// TestStdlibLowercase tests that all standard library package names are717// lowercase. See Issue 40065.718func TestStdlibLowercase(t *testing.T) {719 if !testenv.HasSrc() {720 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)721 }722 ctxt := Default723 all, err := listStdPkgs(ctxt.GOROOT)724 if err != nil {725 t.Fatal(err)726 }727 for _, pkgname := range all {728 if strings.ToLower(pkgname) != pkgname {729 t.Errorf("package %q should not use upper-case path", pkgname)730 }731 }732}733// TestFindImports tests that findImports works. See #43249.734func TestFindImports(t *testing.T) {735 if !testenv.HasSrc() {736 // Tests run in a limited file system and we do not737 // provide access to every source file.738 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)739 }740 imports, err := findImports("go/build")741 if err != nil {742 t.Fatal(err)743 }744 t.Logf("go/build imports %q", imports)745 want := []string{"bytes", "os", "path/filepath", "strings"}746wantLoop:747 for _, w := range want {748 for _, imp := range imports {749 if imp == w {750 continue wantLoop751 }752 }753 t.Errorf("expected to find %q in import list", w)754 }755}...

Full Screen

Full Screen

commands.go

Source:commands.go Github

copy

Full Screen

...28var (29 supportedCommands = []string{CmdStart, CmdPrice, CmdAdd, CmdRemove, CmdMy}30)31const (32 HelpText = `Привет! Я помогу тебе следить за состоянием твооего портфеля акций.33Для взаимодействия выбери одну из доступных команд.`34 EnterSymbolText = "Введите название символа"35 UnsupportedTransitionText = "Недопустимый ввод"36 InternalErrorText = "Внутренняя ошибка сервиса"37 PriceText = "Цена символа `%s`: %.2f"38 SuccessText = "Успешно"39 NoSymbolsText = "Нет добавленных символов"40)41type Processor struct {42 m *fsm.FSM43 chatID int6444 messages chan<- Reply45 provider stocks.Provider46 storage storage.Storage47 success prometheus.Counter48 error prometheus.Counter49}50type IncomingMessage struct {51 Command string52 Message string53}54type Reply struct {55 ChatID int6456 Message string57}58func NewProcessor(provider stocks.Provider, storage storage.Storage, chatID int64,59 messages chan<- Reply, successReplies, errorReplies prometheus.Counter) *Processor {60 p := &Processor{61 messages: messages,62 provider: provider,63 chatID: chatID,64 storage: storage,65 success: successReplies,66 error: errorReplies,67 }68 p.m = fsm.NewFSM(69 Init,70 fsm.Events{71 {Name: CmdStart, Src: []string{Init}, Dst: Init},72 {Name: CmdPrice, Src: []string{Init}, Dst: PriceWaitSymbol},73 {Name: CmdAdd, Src: []string{Init}, Dst: AddWaitSymbol},74 {Name: CmdRemove, Src: []string{Init}, Dst: RemoveWaitSymbol},75 {Name: CmdMy, Src: []string{Init}, Dst: Init},76 {Name: PriceEnterSymbol, Src: []string{PriceWaitSymbol}, Dst: Init},77 {Name: AddEnterSymbol, Src: []string{AddWaitSymbol}, Dst: Init},78 {Name: RemoveEnterSymbol, Src: []string{RemoveWaitSymbol}, Dst: Init},79 },80 fsm.Callbacks{81 CmdStart: p.replyWithText(HelpText),82 CmdPrice: p.replyWithText(EnterSymbolText),83 CmdAdd: p.replyWithText(EnterSymbolText),84 CmdRemove: p.replyWithText(EnterSymbolText),85 AddEnterSymbol: p.addSymbol,86 RemoveEnterSymbol: p.removeSymbol,87 CmdMy: p.showMy,88 PriceEnterSymbol: p.writePrice,89 "enter_state": p.saveState,90 },91 )92 state, _ := p.storage.GetState(chatID)93 if state != "" {94 p.m.SetState(state)95 }96 return p97}98func (p *Processor) Process(m IncomingMessage) {99 if m.Command != "" {100 for _, c := range supportedCommands {101 if c != m.Command {102 continue103 }104 if !p.m.Can(m.Command) {105 p.messages <- p.toReply(UnsupportedTransitionText)106 p.m.SetState(Init)107 return108 }109 if err := p.m.Event(m.Command); err != nil && !errors.As(err, &fsm.NoTransitionError{}) {110 // Unexpected code branching111 log.Printf("[ERROR] Failed to make transition: %+v", err)112 p.messages <- p.toReply(InternalErrorText)113 p.m.SetState(Init)114 return115 }116 return117 }118 p.messages <- p.toReply(UnsupportedTransitionText)119 p.m.SetState(Init)120 return121 }122 if p.m.Current() == Init {123 p.messages <- p.toReply(HelpText)124 return125 }126 transitions := p.m.AvailableTransitions()127 if len(transitions) != 1 {128 log.Printf("[ERROR] More than one transition for text")129 p.messages <- p.toReply(InternalErrorText)130 p.m.SetState(Init)131 return132 }133 if err := p.m.Event(transitions[0], m.Message); err != nil {134 p.messages <- p.toReply(InternalErrorText)135 p.m.SetState(Init)136 return137 }138}139func (p *Processor) replyWithText(text string) fsm.Callback {140 return func(_ *fsm.Event) {141 p.messages <- p.toReply(text)142 }143}144func (p *Processor) writePrice(event *fsm.Event) {145 if len(event.Args) != 1 {146 log.Printf("[ERROR] Incorrect count of args")147 p.messages <- p.toReply(InternalErrorText)148 return149 }150 symbol := strings.TrimSpace(event.Args[0].(string))151 price, err := p.provider.CurrentPrice(symbol)152 if err != nil {153 log.Printf("[ERROR] Failed to get price: %+v", err)154 p.messages <- p.toReply(InternalErrorText)155 return156 }157 p.messages <- p.toReply(fmt.Sprintf(PriceText, symbol, price))158}159func (p *Processor) addSymbol(event *fsm.Event) {160 if len(event.Args) != 1 {161 log.Printf("[ERROR] Incorrect count of args")162 p.messages <- p.toReply(InternalErrorText)163 return164 }165 symbol := strings.TrimSpace(event.Args[0].(string))166 if err := p.storage.AddSymbol(p.chatID, symbol); err != nil {167 log.Printf("[ERROR] Failed to save symbol")168 p.messages <- p.toReply(InternalErrorText)169 return170 }171 p.messages <- p.toReply(SuccessText)172}173func (p *Processor) removeSymbol(event *fsm.Event) {174 if len(event.Args) != 1 {175 log.Printf("[ERROR] Incorrect count of args")176 p.messages <- p.toReply(InternalErrorText)177 return178 }179 symbol := strings.TrimSpace(event.Args[0].(string))180 if err := p.storage.RemoveSymbol(p.chatID, symbol); err != nil {181 log.Printf("[ERROR] Failed to save symbol")182 p.messages <- p.toReply(InternalErrorText)183 return184 }185 p.messages <- p.toReply(SuccessText)186}187func (p *Processor) showMy(event *fsm.Event) {188 symbols, err := p.storage.Symbols(p.chatID)189 if err != nil {190 log.Printf("[ERROR] Failed to get symbols: %+v", err)191 p.messages <- p.toReply(InternalErrorText)192 return193 }194 if len(symbols) == 0 {195 p.messages <- p.toReply(NoSymbolsText)196 return197 }198 var text strings.Builder199 for _, symbol := range symbols {200 price, err := p.provider.CurrentPrice(symbol)201 if err != nil {202 log.Printf("[ERROR] Failed to get price: %+v", err)203 p.messages <- p.toReply(InternalErrorText)204 return205 }206 text.WriteString(fmt.Sprintf(PriceText+"\n", symbol, price))207 }208 p.messages <- p.toReply(text.String())209}210func (p *Processor) toReply(text string) Reply {211 if text == InternalErrorText || text == UnsupportedTransitionText {212 p.error.Inc()213 } else {214 p.success.Inc()215 }216 return Reply{217 ChatID: p.chatID,218 Message: text,219 }220}221func (p *Processor) saveState(event *fsm.Event) {222 if err := p.storage.SetState(p.chatID, event.Dst); err != nil {223 log.Printf("[ERROR] Failed to save state: %+v", err)224 }225}...

Full Screen

Full Screen

Text

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, World!")4}5import (6func main() {7 fmt.Println("Hello, World!")8 fmt.Println(internal.Text())9}10import (11func main() {12 fmt.Println("Hello, World!")13 fmt.Println(internal.text)14}15import (16func main() {17 fmt.Println("Hello, World!")18 fmt.Println(internal.Text())19}20import (21func main() {22 fmt.Println("Hello, World!")23 fmt.Println(internal.Text())24}25import (26func main() {27 fmt.Println("Hello, World!")28}29import (30func main() {31 fmt.Println("Hello, World!")32 fmt.Println(internal.Text())33}34import (35func main() {36 fmt.Println("Hello, World!")37 fmt.Println(internal.Text())38}39import (40func main() {41 fmt.Println("Hello, World!")42 fmt.Println(internal.Text())43}44import (45func main() {46 fmt.Println("Hello, World!")47 fmt.Println(internal.Text())48}49import (50func main() {51 fmt.Println("Hello, World!")52 fmt.Println(internal.Text())53}

Full Screen

Full Screen

Text

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 internal.Text()4}5import (6func Text() {7 fmt.Println("Hello World")8}

Full Screen

Full Screen

Text

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, World!")4}5import "fmt"6import "github.com/username/HelloWorld/1"7func main() {8 fmt.Println("Hello, World!")9}10import "fmt"11import "github.com/username/HelloWorld/1"12func main() {13 fmt.Println(1.Text())14}15import "fmt"16import "github.com/username/HelloWorld/1"17func main() {18 fmt.Println(1.Text())19}20import "fmt"21import "github.com/username/HelloWorld/1"22func main() {23 fmt.Println(1.Text())24}25import "fmt"26import "github.com/username/HelloWorld/1"27func main() {28 fmt.Println(1.Text())29}30import "fmt"31import "github.com/username/HelloWorld/1"32func main() {33 fmt.Println(1.Text())34}35import "fmt"36import "github.com/username/HelloWorld/1"37func main() {38 fmt.Println(1.Text())39}40import "fmt"41import "github.com/username/HelloWorld/1"42func main() {43 fmt.Println(1.Text())44}45import "fmt"46import "github.com/username/HelloWorld/1"47func main()

Full Screen

Full Screen

Text

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "internal"3func main() {4 fmt.Println(internal.Text())5}6func Text() string {7}8import "testing"9func TestText(t *testing.T) {10 if Text() != "Hello World"{11 t.Errorf("Text() = %s; want Hello World", Text())12 }13}14import "testing"15func BenchmarkText(b *testing.B) {16 for i := 0; i < b.N; i++ {17 Text()18 }19}

Full Screen

Full Screen

Text

Using AI Code Generation

copy

Full Screen

1import "github.com/rahulbharadwaj/1/2"2func main() {3 a.Text()4}5import "github.com/rahulbharadwaj/1/2"6func main() {7 a.Text()8}9import "github.com/rahulbharadwaj/1/2"10func main() {11 a.Text()12}13import "github.com/rahulbharadwaj/1/2"14func main() {15 a.Text()16}17import "github.com/rahulbharadwaj/1/2"18func main() {19 a.Text()20}21import "github.com/rahulbharadwaj/1/2"22func main() {23 a.Text()24}25import "github.com/rahulbharadwaj/1/2"26func main() {27 a.Text()28}29import "github.com/rahulbharadwaj/1/2"30func main() {31 a.Text()32}33import "github.com/rahulbharadwaj/1/2"34func main() {35 a.Text()36}37import "github.com/rahulbharadwaj/1/2"38func main() {

Full Screen

Full Screen

Text

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(external.Text())4}5import (6func Text() string {7 return fmt.Sprintln(internal.Text())8}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Ginkgo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful