Как разобрать целое число из ожидаемого теста mySQL ответа? - PullRequest
0 голосов
/ 06 мая 2019

Кто-нибудь знает, как я могу проанализировать целое число из следующего вывода консоли в ожидаемом тесте?

  +-----------------+
  | static_route_id |
  +-----------------+
  |             314 |
  +-----------------+

Что я хотел бы сделать, это

    proc testRouteId { identity_regex } {

          #sign into database (got it)

          #fetch routes with matching identity_regex (a column in the database)
          send { select static_route_id from static_routes where identity_regex="$identity_regex"; }
          send "\r"

          #parse the routeId out of the console output somehow
          expect {
            timeout { send_user "fetchStaticRouteId timed out\n"; return 0 }
            eof { send_user "fetchStaticRouteId failed\n"; return 0 }

          =========Stuck on the regex =========
            -re "REGEX?" { send_user "fetchStaticRouteId $expect_out(1, string)\n" }
          }
          return routeId; # (an int)
        }

Ответы [ 2 ]

0 голосов
/ 10 мая 2019

Вот решение

  proc itemId { databaseName idRegex } {
      set query = "select itemId from tableName where idRegex = '$idRegex';"
      spawn -noecho mysql -uUSERNAME -pPASSWORD $databaseName -B -e "$query"
      expect {
        timeout {send_user "itemId timed out"; return 0 }
        eof {send_user "itemId failed"; return 0}
        "routeId" {
          expect {
            -re { *(\d+).*} { set itemId $expect_out(1,string) }
          }
        } 
      }
      return $itemId
    }
0 голосов
/ 06 мая 2019

Используйте команду expect для сопоставления с шаблоном регулярных выражений, запишите последовательность цифр, окруженных трубами и пробелами, и извлеките цифры из массива expect_out.

Здесь я использую команду Tcl format (например, sprintf) для упрощения работы со строкой отправки. Ваша команда не расширит переменную, потому что вы используете фигурные скобки - см. https://tcl.tk/man/tcl8.6/TclCmd/Tcl.htm правило № 6.

send [format {select static_route_id from static_routes where identity_regex="%s";\r} $identity_regex]
expect -re {\|\s+(\d+)\s+\|.*}
return $expect_out(1,string)
...