Skip to content

JaewooJoung/julia

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

55 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ The Ultimate Guide to Julia Modules, Testing & Package Creation

Welcome to your complete adventure in creating, testing, and publishing Julia modules! This guide combines everything you need to know about building professional Julia packages.

๐Ÿ“š Table of Contents

Creating Your First Module

Basic Module Structure

# src/MyAwesomeGame.jl
module MyAwesomeGame

# 1. Import statements
using DataFrames, Statistics

# 2. Include sub-modules
include("types.jl")
include("functions.jl")

# 3. Export public interfaces
export Player, create_player, 
       punch, kick

end

Types and Functions

# src/types.jl
struct Player
    name::String
    health::Int
    strength::Int
end

# src/functions.jl
function create_player(name::String)
    if isempty(name)
        throw(ArgumentError("Player name cannot be empty"))
    end
    return Player(name, 100, 10)
end

function punch()
    return "POW! ๐Ÿ‘Š"
end

function kick()
    return "WHAM! ๐Ÿฆถ"
end

Package Structure

Create your package structure like this:

MyAwesomeGame/
โ”œโ”€โ”€ Project.toml
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ MyAwesomeGame.jl
โ”‚   โ”œโ”€โ”€ types.jl
โ”‚   โ””โ”€โ”€ functions.jl
โ””โ”€โ”€ test/
    โ”œโ”€โ”€ runtests.jl
    โ”œโ”€โ”€ types_tests.jl
    โ””โ”€โ”€ functions_tests.jl

Project Configuration

Creating Project.toml

name = "MyAwesomeGame"
uuid = "12345678-1234-5678-1234-567812345678"
authors = ["Your Name <your.email@example.com>"]
version = "0.1.0"

[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

[compat]
julia = "1.6"
DataFrames = "1.0"

Generating UUID

# In Julia REPL
using UUIDs
uuid4()  # Copy this UUID to your Project.toml

Testing Your Package

Main Test File (test/runtests.jl)

using Test
using MyAwesomeGame

# Include all test files
include("types_tests.jl")
include("functions_tests.jl")

@testset "MyAwesomeGame.jl" begin
    @testset "Basic Functionality" begin
        @test punch() == "POW! ๐Ÿ‘Š"
        @test kick() == "WHAM! ๐Ÿฆถ"
    end
end

Type Tests (test/types_tests.jl)

@testset "Player Types" begin
    # Test player creation
    player = create_player("TestHero")
    @test player.name == "TestHero"
    @test player.health == 100
    @test player.strength == 10
    
    # Test invalid creation
    @test_throws ArgumentError create_player("")
end

Function Tests (test/functions_tests.jl)

@testset "Game Functions" begin
    @test punch() == "POW! ๐Ÿ‘Š"
    @test kick() == "WHAM! ๐Ÿฆถ"
    
    # Test player functions
    player = create_player("TestHero")
    @test player.name == "TestHero"
end

Advanced Topics

Package Development Workflow

  1. Create New Package

    # In Julia REPL
    ] generate MyAwesomeGame
  2. Develop Package

    # In Julia REPL
    ] dev /path/to/MyAwesomeGame
  3. Test Package

    ] test MyAwesomeGame

Best Testing Practices

  1. Group Related Tests

    @testset "Feature Group" begin
        @testset "Subfeature 1" begin
            # Tests
        end
        @testset "Subfeature 2" begin
            # Tests
        end
    end
  2. Test Different Cases

    @testset "Input Validation" begin
        # Normal cases
        @test valid_function(valid_input) == expected_output
        
        # Edge cases
        @test valid_function(0) == 0
        
        # Error cases
        @test_throws ErrorType invalid_function(bad_input)
    end
  3. Test Floating Point Comparisons

    @test isapprox(compute_float(), expected, rtol=1e-10)

Common Issues & Solutions

1. Module Not Found

# Add your package to LOAD_PATH
push!(LOAD_PATH, "@.")
using MyAwesomeGame

2. Test Dependencies Missing

Add to Project.toml:

[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

3. Package Version Conflicts

# In Julia REPL
] resolve

Quick Reference

Essential Commands

# Package commands (in REPL)
] generate PackageName     # Create new package
] dev PackageName         # Develop package
] test PackageName        # Run tests
] add PackageName         # Install package

# Testing macros
@test                     # Basic assertion
@test_throws             # Error testing
@test_warn               # Warning testing
@test_logs               # Log testing
@test_broken             # Known failing tests
@test_skip               # Skip tests

Module Organization Checklist

  • Project.toml configured
  • UUID generated
  • Dependencies listed
  • Source files organized
  • Tests written
  • Documentation added
  • README updated
  • LICENSE added

Contributing

Feel free to submit issues and enhancement requests!

License

This project is licensed under the MIT License - see the LICENSE file for details.


Happy coding! May your tests be green and your builds be clean! ๐Ÿš€

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published