Razor Pages - Overview

Razor pages provides a way to build web applications using a page-focused approach. Under the hood, it uses the same routing and middleware as MVC, and it provides directory-based conventions for views. Pages can have a viewmodel where we can define page data and actions. Pages: Pages are the main part of a Razor Pages application. Pages are created in the Pages directory and are named with the .cshtml extension. Each page can have a corresponding .cshtml.cs file which contains the page model. Page Model: The page model is responsible for handling requests and rendering the page. It is a class that contains the data and actions for the page. The page model is defined in a .cshtml.cs file and is associated with a .cshtml file. Getting Started Create a new project using the dotnet command-line interface. ...

Dotnet MVC - Overview

C# asp.net MVC is a web application framework that provides a model-view-controller architecture. The MVC pattern separates the application into three main components: the model, the view, and the controller. This separation of concerns makes it easier to manage and maintain the application. This structure also makes it easier to test and debug the application. Model: The model represents the data and core business logic related to data of the application. It is responsible for managing the data and the rules for accessing and updating the data. The model is independent of the user interface and can be reused across different views. View: The view is responsible for presenting the data to the user, think of HTML Templates. It is the UI of the application and is responsible for rendering the data to the user. The view is independent of the model and the controller and can be reused across different models and controllers. Controller: The controller is primarily responsible for handling user input and requests. It processes the input, interacts with the model, and selects the view to be rendered. The controller is responsible for the flow of the application and is the glue between the model and the view. Underneath, it uses kestrel server to host the application and uses asp.net core for web development. ...

Scala - Testing

Scala supports testing using the JUnit and ScalaTest frameworks. These frameworks provide a way to write and run tests for your Scala code. JUnit is a popular choice for unit testing in Scala due to its simplicity and extensibility. Writing Tests JUnit provides a set of annotations that can be used to write tests. The @Test annotation is used to mark a method as a test method. The @Before and @After annotations are used to mark methods that should be run before and after each test method, respectively. ...

Rust - Testing

Rust has built-in support for testing with the cargo and assert. We can write tests in Rust using the #[test] attribute and the assert! macro. The cargo command-line interface can be used to run the tests. The tests can be written in the same file as the code being tested, or in a separate file. Writing Tests Suppose we have a function add in main.rs which adds two numbers. // main.rs pub fn add(a: i32, b: i32) -> i32 { a + b } We can write a test for this function in the same file. ...

Python - Testing

Python unittest library is a built-in library that provides a way to write and run tests for your Python code. It is a part of the Python standard library and is widely used for writing unit tests in Python. pytest is another popular choice for testing in Python due to its simplicity and extensibility. Writing Tests unittest provides a set of classes and methods that can be used to write tests. The unittest.TestCase class is used to define test cases, and the assert methods are used to verify the expected behavior of the code under test. ...

Kotlin - Testing

Writing tests in Kotlin can be done using the JUnit and KotlinTest frameworks. These frameworks provide a way to write and run tests for your Kotlin code. JUnit is a popular choice for unit testing in Kotlin due to its simplicity and extensibility. Writing Tests JUnit provides a set of annotations that can be used to write tests. The @Test annotation is used to mark a method as a test method. The @Before and @After annotations are used to mark methods that should be run before and after each test method, respectively. ...

C# - Testing

C#, NUnit has a lot of features that makes it a good choice for testing. It provides features for writing and running tests, and also for creating test fixtures and test suites. NUnit is a popular choice for unit testing in C# due to its simplicity and extensibility. Writing Tests NUnit provides a set of attributes that can be used to write tests. The Test attribute is used to mark a method as a test method. The TestCase attribute is used to provide test data for parameterized tests. The SetUp and TearDown attributes are used to mark methods that should be run before and after each test method, respectively. ...

Go - Testing

Unit tests in Go can be written using testing package. We require to create a file with _test.go suffix to write tests for a package. Conventionally, the test file should be in the same package as the code being tested. The test file should import the testing package and the package being tested. eg: . ├── main.go └── main_test.go Writing Tests Suppose we have a function Add in main.go which adds two numbers. ...

Parser - An Example in Go

A Parser is a program that reads the tokens and converts it into an abstract syntax tree (AST). It is the second part of an interpreter. What the usage looks like // for an input `let five = 5;` input := `let five = 5;` l := lexer.New(input) p := parser.New(l) // result is a list of statements result := []ast.Statement{ &ast.LetStatement{ Token: token.Token{Type: token.LET, Literal: "let"}, Name: &ast.Identifier{ Token: token.Token{Type: token.IDENT, Literal: "five"}, Value: "five", }, Value: &ast.IntegerLiteral{ Token: token.Token{Type: token.INT, Literal: "5"}, Value: 5, }, }, } input2 := "1 + 2 + 3;" l2 := lexer.New(input2) p2 := parser.New(l2) result2 := []ast.Statement{ &ast.ExpressionStatement{ Token: token.Token{Type: token.INT, Literal: "1"}, Expression: &ast.InfixExpression{ Token: token.Token{Type: token.PLUS, Literal: "+"}, Left: &ast.IntegerLiteral{ Token: token.Token{Type: token.INT, Literal: "1"}, Value: 1, }, Operator: "+", Right: &ast.InfixExpression{ Token: token.Token{Type: token.PLUS, Literal: "+"}, Left: &ast.IntegerLiteral{ Token: token.Token{Type: token.INT, Literal: "2"}, Value: 2, }, Operator: "+", Right: &ast.IntegerLiteral{ Token: token.Token{Type: token.INT, Literal: "3"}, Value: 3, }, }, }, }, } The parser also needs to handle operator precedence and associativity. It is a complex part of an interpreter. ...

Lexer - An Example in Go

A Lexer is a program that reads the code and converts it into tokens. It is the first part of an interpreter. What the usage looks like // for an input `let five = 5;` input := `let five = 5;` l := lexer.New(input) // result is a list of tokens result := []tokens.Token{ {Type: tokens.LET, Literal: "let"}, {Type: tokens.IDENT, Literal: "five"}, {Type: tokens.ASSIGN, Literal: "="}, {Type: tokens.INT, Literal: "5"}, {Type: tokens.SEMICOLON, Literal: ";"}, {Type: tokens.EOF, Literal: ""}, } input2 := "1 + 2 + 3;" l2 := lexer.New(input2) result2 := []tokens.Token{ {Type: tokens.INT, Literal: "1"}, {Type: tokens.PLUS, Literal: "+"}, {Type: tokens.INT, Literal: "2"}, {Type: tokens.PLUS, Literal: "+"}, {Type: tokens.INT, Literal: "3"}, {Type: tokens.SEMICOLON, Literal: ";"}, {Type: tokens.EOF, Literal: ""}, } Let’s see how to implement a sample lexer in Go. ...