diff options
Diffstat (limited to 'js/scripting-lang/tests/17_real_world_scenarios.txt')
-rw-r--r-- | js/scripting-lang/tests/17_real_world_scenarios.txt | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/js/scripting-lang/tests/17_real_world_scenarios.txt b/js/scripting-lang/tests/17_real_world_scenarios.txt new file mode 100644 index 0000000..0a9fc49 --- /dev/null +++ b/js/scripting-lang/tests/17_real_world_scenarios.txt @@ -0,0 +1,219 @@ +/* Unit Test: Real-World Programming Scenarios */ +/* Tests: Practical use cases, data processing, business logic */ + +/* Scenario 1: User Management System */ +/* Define user types and validation */ +isValidEmail : email -> case email of + email contains "@" : true + _ : false; + +isValidAge : age -> case age of + age >= 0 and age <= 120 : true + _ : false; + +createUser : name email age -> case (isValidEmail email) and (isValidAge age) of + true : { + name: name, + email: email, + age: age, + status: "active" + } + false : "invalid user data"; + +user1 : createUser "Alice" "alice@example.com" 25; +user2 : createUser "Bob" "invalid-email" 30; +user3 : createUser "Charlie" "charlie@test.com" 150; + +..assert user1.name = "Alice"; +..assert user2 = "invalid user data"; +..assert user3 = "invalid user data"; + +/* Scenario 2: Shopping Cart System */ +/* Product definitions */ +product1 : {id: 1, name: "Laptop", price: 999.99, category: "electronics"}; +product2 : {id: 2, name: "Book", price: 19.99, category: "books"}; +product3 : {id: 3, name: "Coffee", price: 4.99, category: "food"}; + +/* Cart operations */ +addToCart : cart product -> { + cart, + product +}; + +calculateTotal : cart -> case cart of + cart is table : cart.product.price + _ : 0; + +applyDiscount : total discount -> case discount of + discount > 0 and discount <= 100 : total * (1 - discount / 100) + _ : total; + +cart : addToCart {} product1; +cart : addToCart cart product2; +total : calculateTotal cart; +discounted : applyDiscount total 10; + +..assert total = 1019.98; +..assert discounted = 917.982; + +/* Scenario 3: Data Processing Pipeline */ +/* Sample data */ +sales_data : { + {month: "Jan", sales: 1000, region: "North"}, + {month: "Feb", sales: 1200, region: "North"}, + {month: "Mar", sales: 800, region: "South"}, + {month: "Apr", sales: 1500, region: "North"}, + {month: "May", sales: 900, region: "South"} +}; + +/* Data processing functions */ +filterByRegion : data region -> case data of + data.region = region : data + _ : null; + +sumSales : data -> case data of + data is table : data.sales + _ : 0; + +calculateAverage : total count -> case count of + count > 0 : total / count + _ : 0; + +/* Process North region sales */ +north_sales : filterByRegion sales_data "North"; +north_total : sumSales north_sales; +north_avg : calculateAverage north_total 3; + +..assert north_total = 3700; +..assert north_avg = 1233.3333333333333; + +/* Scenario 4: Configuration Management */ +/* Environment configuration */ +getConfig : env -> case env of + "development" : { + database: "dev_db", + port: 3000, + debug: true, + log_level: "debug" + } + "production" : { + database: "prod_db", + port: 80, + debug: false, + log_level: "error" + } + "testing" : { + database: "test_db", + port: 3001, + debug: true, + log_level: "info" + } + _ : "unknown environment"; + +dev_config : getConfig "development"; +prod_config : getConfig "production"; + +..assert dev_config.debug = true; +..assert prod_config.debug = false; +..assert dev_config.port = 3000; +..assert prod_config.port = 80; + +/* Scenario 5: Error Handling and Recovery */ +/* Robust function with error handling */ +safeDivide : x y -> case y of + 0 : "division by zero error" + _ : x / y; + +safeParseNumber : str -> case str of + str is number : str + _ : "invalid number"; + +processData : data -> case data of + data is number : data * 2 + data is string : safeParseNumber data + _ : "unsupported data type"; + +safe_result1 : safeDivide 10 2; +safe_result2 : safeDivide 10 0; +safe_result3 : processData 5; +safe_result4 : processData "abc"; + +..assert safe_result1 = 5; +..assert safe_result2 = "division by zero error"; +..assert safe_result3 = 10; +..assert safe_result4 = "invalid number"; + +/* Scenario 6: Event Handling System */ +/* Event types and handlers */ +eventHandlers : { + "user.login": x -> "User logged in: " + x, + "user.logout": x -> "User logged out: " + x, + "order.created": x -> "Order created: " + x, + "order.completed": x -> "Order completed: " + x +}; + +handleEvent : event data -> case eventHandlers[event] of + handler : handler data + _ : "Unknown event: " + event; + +login_event : handleEvent "user.login" "alice@example.com"; +logout_event : handleEvent "user.logout" "bob@example.com"; +unknown_event : handleEvent "unknown.event" "data"; + +..assert login_event = "User logged in: alice@example.com"; +..assert logout_event = "User logged out: bob@example.com"; +..assert unknown_event = "Unknown event: unknown.event"; + +/* Scenario 7: Caching System */ +/* Simple cache implementation */ +cache : {}; + +setCache : key value -> cache[key] = value; +getCache : key -> case cache[key] of + undefined : "not found" + value : value; +clearCache : key -> cache[key] = undefined; + +setCache "user.1" "Alice"; +setCache "user.2" "Bob"; +cache_result1 : getCache "user.1"; +cache_result2 : getCache "user.999"; +clearCache "user.1"; +cache_result3 : getCache "user.1"; + +..assert cache_result1 = "Alice"; +..assert cache_result2 = "not found"; +..assert cache_result3 = "not found"; + +/* Scenario 8: API Response Processing */ +/* Mock API responses */ +apiResponse : { + status: 200, + data: { + users: { + {id: 1, name: "Alice", active: true}, + {id: 2, name: "Bob", active: false}, + {id: 3, name: "Charlie", active: true} + }, + total: 3 + } +}; + +processApiResponse : response -> case response.status of + 200 : response.data + 404 : "not found" + 500 : "server error" + _ : "unknown status"; + +getActiveUsers : data -> case data.users of + users : case users.active of + true : users + _ : null; + +api_data : processApiResponse apiResponse; +active_users : getActiveUsers api_data; + +..assert api_data.total = 3; +..assert active_users = null; /* Simplified for this example */ + +..out "Real-world scenarios test completed successfully"; \ No newline at end of file |