-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
var support json substituion and expression substition
- Loading branch information
Showing
10 changed files
with
303 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
from enum import Enum | ||
|
||
|
||
class TokenType(Enum): | ||
VAR = 1 | ||
OPERATOR = 2 | ||
NUMBER = 3 | ||
PARENTHESES = 4 | ||
|
||
class Token(): | ||
def __init__(self, token_type, value): | ||
self.token_type = token_type | ||
self.value = value | ||
|
||
def __repr__(self): | ||
return f"{self.token_type}({self.value})" | ||
|
||
def __eq__(self, compr): | ||
if self.value == compr.value and self.token_type == compr.token_type: | ||
return True | ||
return False | ||
|
||
@staticmethod | ||
def var(value): | ||
return Token(TokenType.VAR, value) | ||
|
||
@staticmethod | ||
def operator(value): | ||
return Token(TokenType.OPERATOR, value) | ||
|
||
@staticmethod | ||
def number(value): | ||
return Token(TokenType.NUMBER, value) | ||
|
||
@staticmethod | ||
def parentheses(value): | ||
return Token(TokenType.PARENTHESES, value) | ||
|
||
@staticmethod | ||
def parse_expr(expr): | ||
current_var = "" | ||
numeric = "" | ||
for i in expr: | ||
if current_var: | ||
if i in ['\t', '\n', ' ']: | ||
continue | ||
elif i in ['+', '-', '*', '/']: | ||
yield Token.var(current_var) | ||
yield Token.operator(i) | ||
current_var = "" | ||
continue | ||
elif i in ['(', ')']: | ||
yield Token.var(current_var) | ||
yield Token.parentheses(i) | ||
current_var = "" | ||
else: | ||
current_var += i | ||
elif numeric: | ||
if i in ['\t', '\n', ' ']: | ||
continue | ||
elif i in ['+', '-', '*', '/']: | ||
yield Token.number(numeric) | ||
yield Token.operator(i) | ||
numeric = "" | ||
continue | ||
elif i in ['(', ')']: | ||
yield Token.number(numeric) | ||
yield Token.parentheses(i) | ||
numeric = "" | ||
else: | ||
numeric += i | ||
elif i in ['+', '-', '*', '/']: | ||
yield Token.operator(i) | ||
elif i in ['(', ')']: | ||
yield Token.parentheses(i) | ||
elif i.isalpha(): | ||
current_var = i | ||
elif i.isnumeric(): | ||
numeric = i | ||
if current_var: | ||
yield Token.var(current_var) | ||
if numeric: | ||
yield Token.number(numeric) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "dothttp-req" | ||
version = "0.0.44a2" | ||
version = "0.0.44a3" | ||
description = "Dothttp is Simple http client for testing and development" | ||
authors = ["Prasanth <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import pytest | ||
from dothttp.parse.expression import Token, TokenType | ||
|
||
def test_var_token(): | ||
token = Token.var("x") | ||
assert token.token_type == TokenType.VAR | ||
assert token.value == "x" | ||
|
||
def test_operator_token(): | ||
token = Token.operator("+") | ||
assert token.token_type == TokenType.OPERATOR | ||
assert token.value == "+" | ||
|
||
def test_number_token(): | ||
token = Token.number("123") | ||
assert token.token_type == TokenType.NUMBER | ||
assert token.value == "123" | ||
|
||
def test_parentheses_token(): | ||
token = Token.parentheses("(") | ||
assert token.token_type == TokenType.PARENTHESES | ||
assert token.value == "(" | ||
|
||
def test_parse_expr_simple(): | ||
expr = "x + 1" | ||
tokens = list(Token.parse_expr(expr)) | ||
print(tokens) | ||
assert tokens == [ | ||
Token.var("x"), | ||
Token.operator("+"), | ||
Token.number("1") | ||
] | ||
|
||
def test_parse_expr_with_parentheses(): | ||
expr = "(x + 1) * y" | ||
tokens = list(Token.parse_expr(expr)) | ||
assert tokens == [ | ||
Token.parentheses("("), | ||
Token.var("x"), | ||
Token.operator("+"), | ||
Token.number("1"), | ||
Token.parentheses(")"), | ||
Token.operator("*"), | ||
Token.var("y") | ||
] | ||
|
||
def test_parse_expr_with_spaces(): | ||
expr = " x + 1 " | ||
tokens = list(Token.parse_expr(expr)) | ||
assert tokens == [ | ||
Token.var("x"), | ||
Token.operator("+"), | ||
Token.number("1") | ||
] | ||
|
||
def test_parse_expr_with_multiple_digits(): | ||
expr = "x + 123" | ||
tokens = list(Token.parse_expr(expr)) | ||
assert tokens == [ | ||
Token.var("x"), | ||
Token.operator("+"), | ||
Token.number("123") | ||
] | ||
|
||
def test_parse_expr_with_multiple_vars(): | ||
expr = "x + y" | ||
tokens = list(Token.parse_expr(expr)) | ||
assert tokens == [ | ||
Token.var("x"), | ||
Token.operator("+"), | ||
Token.var("y") | ||
] | ||
|
||
def test_parse_expr_edge_case_empty(): | ||
expr = "" | ||
tokens = list(Token.parse_expr(expr)) | ||
assert tokens == [] | ||
|
||
def test_parse_expr_edge_case_only_operators(): | ||
expr = "+-*/" | ||
tokens = list(Token.parse_expr(expr)) | ||
assert tokens == [ | ||
Token.operator("+"), | ||
Token.operator("-"), | ||
Token.operator("*"), | ||
Token.operator("/") | ||
] | ||
|
||
def test_parse_expr_edge_case_only_parentheses(): | ||
expr = "()" | ||
tokens = list(Token.parse_expr(expr)) | ||
assert tokens == [ | ||
Token.parentheses("("), | ||
Token.parentheses(")") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Setting variables | ||
var num1 = 10.1; | ||
var num2 = 5.2; | ||
|
||
# Addition | ||
var sum = (num1 + num2); | ||
|
||
# Subtraction | ||
var difference = (num1 - num2); | ||
|
||
# Multiplication | ||
var product = (num1 * num2); | ||
|
||
# Division | ||
var quotient = (num1 / num2); | ||
|
||
var test = "This is a test"; | ||
|
||
# Output the results | ||
POST http://localhost:8000/post | ||
json( | ||
{ | ||
"sum": {{sum}}, | ||
"difference": {{difference}}, | ||
"product": {{product}}, | ||
"quotient": {{quotient}}, | ||
"test": "{{test}}" | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var a = 10; | ||
var b = 11.1; | ||
var c = (10 + 11.1); | ||
var d = "hello world"; | ||
var e = $"if is a={a}, b={b} and a + b is {c}"; | ||
var f = true; | ||
var g = null; | ||
var h = { | ||
"a": {{a}}, | ||
"b": {{b}}, | ||
"c": {{c}}, | ||
"d": {{d}}, | ||
"e": {{e}}, | ||
"f": {{f}}, | ||
"g": {{g}} | ||
}; | ||
var e = { | ||
"a":{ | ||
"b": { | ||
"d": { | ||
"e": {{h}} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
POST "https://req.dothttp.dev/" | ||
json( | ||
{{e}} | ||
) | ||
|
||
|