https://github.com/postmodern/fake_io.rb
A mixin module for creating fake IO-like classes.
https://github.com/postmodern/fake_io.rb
help-wanted io mocking ruby
Last synced: 9 months ago
JSON representation
A mixin module for creating fake IO-like classes.
- Host: GitHub
- URL: https://github.com/postmodern/fake_io.rb
- Owner: postmodern
- License: mit
- Created: 2021-12-21T03:25:18.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-02-01T23:57:40.000Z (almost 2 years ago)
- Last Synced: 2025-04-18T03:02:18.351Z (9 months ago)
- Topics: help-wanted, io, mocking, ruby
- Language: Ruby
- Homepage:
- Size: 197 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 24
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# fake_io
[](https://github.com/postmodern/fake_io.rb/actions/workflows/ruby.yml)
[](https://badge.fury.io/rb/fake_io)
* [Source](https://github.com/postmodern/fake_io.rb)
* [Issues](https://github.com/postmodern/fake_io.rb/issues)
* [Documentation](https://rubydoc.info/gems/fake_io)
## Description
{FakeIO} is a mixin module for creating fake [IO]-like classes.
## Features
* Supports all of the Ruby 2.x and 3.x [IO] instance methods.
* Emulates buffered I/O.
* UTF-8 aware.
* Can be included into any Class.
* Zero dependencies.
## Requirements
* [Ruby] >= 2.0.0
[Ruby]: https://www.ruby-lang.org/
## Install
```shell
$ gem install fake_io
```
### gemspec
```ruby
gem.add_dependency 'fake_io', '~> 1.0'
```
### Gemfile
```ruby
gem 'fake_io', '~> 1.0'
```
## Examples
```ruby
require 'fake_io'
class FakeFile
include FakeIO
def initialize(chunks=[])
@index = 0
@chunks = chunks
io_initialize
end
protected
def io_read
unless (block = @chunks[@index])
raise(EOFError,"end of stream")
end
@index += 1
return block
end
def io_write(data)
@chunks[@index] = data
@index += 1
return data.length
end
end
file = FakeFile.new(["one\ntwo\n", "three\nfour", "\n"])
file.readlines
# => ["one\n", "two\n", "three\n", "four\n"]
```
## Copyright
See {file:LICENSE.txt} for details.
[IO]: https://rubydoc.info/stdlib/core/IO